Skip to content

Instantly share code, notes, and snippets.

@moisoto
Last active August 17, 2025 03:58
Show Gist options
  • Select an option

  • Save moisoto/605e157bbbaa6906e40530221c683ffa to your computer and use it in GitHub Desktop.

Select an option

Save moisoto/605e157bbbaa6906e40530221c683ffa to your computer and use it in GitHub Desktop.
Precompile and use standard modules

Precompile and use standard modules

This example will compile the following module files:

  • iostream.gcm
  • utility.gcm
  • vector.gcm
  • algorithm.gcm
  • functional.gcm
  • string.gcm
  • memory.gcm
  • print.gcm

Requirements

Please make sure you have docker installed and have the latest gcc official image:

docker pull gcc

Compiling modules

The script docker_cmp-modules.sh will compile the modules using the docker container. They will be located on your current directory after execution. Specifically at the folder named gcm.cache

The command used to complile them is the following:

g++ -std=c++23 -fmodules-ts -x c++-system-header iostream \
  -x c++-system-header vector -x c++-system-header string \
  -x c++-system-header algorithm -x c++-system-header memory \
  -x c++-system-header utility -x c++-system-header functional \
  -x c++-system-header print

Running the Hello World sample

To compile and run the sample program use the provided ./docker_run.sh script. It will compile main.cpp as follows:

g++ -std=c++23 -fmodules-ts main.cpp -o myApp
#!/bin/zsh
set -e # Stop on errors
docker run --rm -v "$PWD":/usr/src/app -w /usr/src/app gcc bash -c "
mkdir -p gcm.cache && chmod 755 gcm.cache && \
g++ -std=c++23 -fmodules-ts -x c++-system-header iostream \
-x c++-system-header vector -x c++-system-header string \
-x c++-system-header algorithm -x c++-system-header memory \
-x c++-system-header utility -x c++-system-header functional \
-x c++-system-header print
"
echo
echo "Modules Build complete!"
echo
echo "Your current compiled modules are:"
find . -name "*.gcm" -exec basename {} \;
#!/bin/zsh
set -e # Stop on errors
mod_exist=$(find . -type f -name "iostream.gcm" -print -quit)
if [[ -z "$mod_exist" ]] ; then
echo "Can't fine iostream.gcm"
echo "Please run: ./docker_cmp-modules.sh"
exit 1
fi
docker run --rm -v "$PWD":/usr/src/app -w /usr/src/app gcc bash -c "
g++ -std=c++23 -fmodules-ts main.cpp -o myApp && ./myApp
"
echo
echo "Build complete! Program was executed."
rm myApp
import <iostream>;
import <print>;
int main() {
std::cout << "Hello World." << std::endl;
std::println("From Docker!");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment