Skip to content

Instantly share code, notes, and snippets.

@moisoto
Created August 17, 2025 04:02
Show Gist options
  • Select an option

  • Save moisoto/2f4a23eb1b0e3a80e59265aad41adddb to your computer and use it in GitHub Desktop.

Select an option

Save moisoto/2f4a23eb1b0e3a80e59265aad41adddb to your computer and use it in GitHub Desktop.
Compile std module for use on Linux / MacOS

Compile std module for use on Linux/MacOS

This example will compile the std module using the official gcc docker image.

Requirements

Make sure you have docker installed and the latest gcc image:

docker pull gcc

Checking the location of std.cc

First run a shell inside a gcc container:

docker run -it --rm gcc /bin/bash

Your will be greeted by a shell prompt from inside the container. Run the following to get the path for std.cc:

find /usr -name std.cc
exit

In the next setion we'll assume the path you got was /usr/local/include/c++/15.2.0/bits/std.cc

Compiling the std module

Now that you know the location of std.cc you can compile it with:

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 -c /usr/local/include/c++/15.2.0/bits/std.cc && \
  rm std.o
"

You can also use the provided docker_cmp-std.sh script, but make sure the correct path is used.

The std.gcm file will be located in your current directory inside the folder gcm.cache.

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 -c /usr/local/include/c++/15.2.0/bits/std.cc
"
rm std.o
echo
echo "Build of Module std complete!"
echo
echo "Your current compiled modules are:"
find . -name "*.gcm"
#!/bin/zsh
set -e # Stop on errors
mod_exist=$(find . -type f -name "std.gcm" -print -quit)
if [[ -z "$mod_exist" ]] ; then
echo "Can't fine std.gcm"
echo "Please run: docker_cmp-std.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 std;
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