This example demonstrates how to use the docker-cross-cpp images to build a C++ application for multiple platforms from a single Dockerfile.
test.cpp: A simple C++ programCMakeLists.txt: CMake build configurationDockerfile: Multi-stage build that cross-compiles the binarydocker-bake.hcl: Docker Bake configuration for building multiple platforms
Build the binary for all configured platforms:
docker buildx bakeThis will create binaries in the build/ directory for:
- Linux: amd64, arm64, armv7, 386, riscv64, s390x, ppc64le
- Windows: amd64, 386, arm64, armv7
- Macos: amd64, arm64
Build for a single platform:
docker buildx build --platform linux/arm64 -o build .Or specify multiple platforms:
docker buildx build --platform linux/amd64,linux/arm64,windows/amd64 -o build .The Dockerfile uses a multi-stage build:
-
Build stage: Uses the appropriate
cross-cppimage based on$TARGETPLATFORMFROM --platform=$BUILDPLATFORM ghcr.io/dseif0x/cross-cpp-$TARGETPLATFORM:latest AS build -
Copy and compile: Copies source files and runs CMake to build the binary
COPY CMakeLists.txt . COPY test.cpp . RUN cmake -S . -B build && cmake --build build
-
Extract binary: The final stage extracts just the compiled binary
The cross-compilation toolchain is automatically configured through:
CMAKE_TOOLCHAIN_FILEenvironment variable (pre-set in the images)- Cross-compiler environment variables (
CC,CXX,AR, etc.)
To adapt this for your own C++ project:
- Replace
test.cppandCMakeLists.txtwith your source files - Update the
Dockerfileto copy your project files - Modify
docker-bake.hclto select which platforms you need - Add any additional dependencies to the Dockerfile build stage
Built binaries are extracted to the build/ directory with platform-specific paths:
build/
linux_amd64/
linux_arm64/
windows_amd64/
...
- The images include CMake 3.20+ and Ninja build system
- CMake toolchain files are pre-configured for each platform
- For macOS targets, you may need to adjust the SDK version in the main
docker-bake.hcl - Windows binaries are built with llvm-mingw and target Windows 7+