Skip to content

Simple C++ Docker-Images with gcc/clang, cmake and dependency manager (conan and/or vcpkg).

License

Notifications You must be signed in to change notification settings

abeimler/simple-cppbuilder

Repository files navigation

Simple C++ Docker Builder

logo
ci license stars docker-image-size automated pulls

Simple C++ Builder with compilers, buildtools and dependency manager.

Features

What's included

Based on archlinux:base-devel with yay.

  • Compilers: clang, gcc or cross-compiler
  • Buildtools: cmake, make and ninja
  • Dependency Manager: conan and/or vcpkg
  • More Tools: python, pip, ccache, cppcheck, doxygen and more

How to use this image

You can find a full C++ project example here.

Add Docker into your C++ Project

  1. Create a Dockerfile in your C++ project
## build stage
FROM abeimler/simple-cppbuilder as build
COPY . .
CMD ["task", "-t", "/home/taskfiles/Taskfile.yml", "build"]

## test stage
FROM build as test
CMD ["task", "-t", "/home/taskfiles/Taskfile.yml", "test"]
  1. Build and run the Docker image:
$ docker build -t my-cpp-project .
$ docker run -it --rm --name my-app my-cpp-project

Run a single file

$ docker run -it --rm --name my-cpp-project -v "$PWD":/home/project -w /home/project abeimler/simple-cppbuilder ./docker-build.sh

Image Variants

:base

Base image with gcc, buildtools and conan (dependency manager) installed.

:<version>, :latest

Default image with gcc, buildtools, conan installed and vcpkg bootstrapped.

Compilers

:clang

Default image with clang compiler.

:gcc10, :gcc9 (outdated)

Default image with gcc compiler.

Libraries

:libcpp

Default image with clang and libc++ installed.

:boost

Default image with boost installed.

:abseil-cpp

Default image with abseil installed.

:opengl-libs

Default image with some OpenGL dependencies: mesa glu glfw-x11 libx11 libxrender libxext libxcursor libxrandr libxinerama xorg-server-devel.

Misc

:ci, :ci-x64-mingw-w64

Alternative Ubuntu-based image with basic compilers and tools.
Nice base image for CI/CD.

:ci-setup-cpp

Alternative Ubuntu-based image with basic compilers and tools.
Uses setup-cpp.

Cross-Compiler (experimental)

Not fully tested

:x64-mingw-w64, :x86-mingw-w64

Default image with mingw-w64-cross-compiler: mingw-w64-gcc and toolchain.

:x64-mingw-w64-mxe, x86-mingw-w64-mxe, :x64-mingw-w64-mxe-static, :x86-mingw-w64-mxe-static (older compiler)

Default image with mxe and toolchain.

:emscripten

Default image with emscripten.

:rpi4, :rpi3, :rpi2, :rpi1

Default image with arm-cross-compiler: (crosstool-ng) for RaspberryPi, raspberrypi-tools and toolchain.

:arm-android, :arm64-android, :x86-android, :x64-android

Default image with android-ndk and toolchain.

More Examples

Dockerfile with system dependencies

FROM abeimler/simple-cppbuilder as base

RUN pacman-db-upgrade && pacman -S --noconfirm  \
    qt5-base qt5-base-util 

# build stage
FROM base as build
COPY . .
CMD ["task", "-t", "/home/taskfiles/Taskfile.yml", "build"]

Dockerfile using AUR

FROM abeimler/simple-cppbuilder as base

# install android-sdk and android-ndk
RUN runuser -l yay -c \
    "yay -Syu --noconfirm && yay -S --noconfirm \
      android-sdk android-ndk"

# build stage
FROM base as build
COPY . .
CMD ["./my-android-build.sh"]

Using docker-compose for build

version: '3.4'

services:
  # gcc Release with Ninja
  gcc-release-build:
    build:
      context: .
      dockerfile: Dockerfile
      target: build
    environment:
      CC: gcc
      CXX: g++
      TARGET: all
      BUILD_TYPE: Release
      CMAKE_GENERATOR: Ninja
$ docker-compose up --build

Using docker-compose for testing

version: '3.4'

services:
  # gcc Debug with Ninja
  gcc-debug-test:
    build:
      context: .
      dockerfile: Dockerfile
      target: test
    environment:
      TARGET: all
      BUILD_TYPE: Debug
      CMAKE_GENERATOR: Ninja
$ docker-compose up --build

Use your custom build script Better use a Taskfile

my-build.sh

Taskfile.yml

---
version: "3"

vars:
  PROJECT_DIR: '{{.PROJECT_DIR | default "."}}'
  TARGET: '{{.TARGET | default "all"}}'
  CMAKE_GENERATOR: '{{.CMAKE_GENERATOR | default "Ninja Multi-Config"}}'
  BUILD_TYPE: '{{.BUILD_TYPE | default "Release"}}'

tasks:
  configure:
    dir: '{{.PROJECT_DIR}}'
    cmds:
      - >
        cmake -B build -S . -G "{{.CMAKE_GENERATOR}}" \
            -DCMAKE_BUILD_TYPE="{{.BUILD_TYPE}}" \
            {{.CMAKE_ARGS}}

  build:
    dir: '{{.PROJECT_DIR}}'
    cmds:
      - task: configure
      - 'cmake --build build --target "{{.TARGET}}"'

  test:
    dir: '{{.PROJECT_DIR}}'
    cmds:
      - task: build
      - ctest --build-test --test-dir build

Dockerfile

# base image
FROM abeimler/simple-cppbuilder as base

# build stage
FROM base as build
COPY . .
#CMD ["./my-build.sh"]
CMD ["task", "build"]

docker-compose.yml

---
version: '3.4'

services:
  my-app-build:
    build:
      context: .
      dockerfile: Dockerfile
      target: build
$ docker-compose up --build

Environment Variables

CC

C Compiler, default: gcc. Can also be clang.

CXX

C++ Compiler, default: g++. Can also be clang++.

CMAKE_GENERATOR

CMake Generator cmake -G, default: Ninja. Can also be Unix Makefiles.

BUILD_TYPE

CMake BuildType -DCMAKE_BUILD_TYPE, default: Release. Can also be Debug, RelWithDebInfo or MinSizeRel.

TARGET

target run by cmake cmake --target, default: all.

TOOLCHAIN_FILE

CMake Toolchain File -DCMAKE_TOOLCHAIN_FILE, default ./vcpkg/scripts/buildsystems/vcpkg.cmake.

CMAKE_ARGS

Custom CMake Arguments, e.g. -DENABLE_TESTING:BOOL=ON -DOPT_ENABLE_COVERAGE:BOOL=ON -DENABLE_DEVELOPER_MODE:BOOL=OFF.

More Environment Variables

Here are some Environment Variables you can use in your build script, such as paths, scripts and programs.

MAKE

make or ninja.

PROJECT_DIR

Project-Home folder, /home/project.

VCPKG_ROOT

Path to vcpkg, /home/project/vcpkg.

VCPKG_TOOLCHAIN_FILE

vcpkg Toolchain, /home/project/vcpkg/scripts/buildsystems/vcpkg.cmake.

CODECOV

Path to codedov uploader, /home/codecov/codecov.

(Used in CI/CD images)

VCPKG_DEFAULT_HOST_TRIPLET, VCPKG_DEFAULT_TRIPLET

vcpkg target- and host-triplet.

(Used in cross-compiler images)

VCPKG_CHAINLOAD_TOOLCHAIN_FILE

alternate CMake toolchain (for vcpkg)

(Used in cross-compiler images)

CROSS_TOOLCHAIN

Path to CMake Toolchain, /home/cmake/x86_64-w64-mingw32.toolchain.cmake.

(Used in cross-compiler images)

SETUP_ENV_SCRIPT

source or setup-environment-script for cross compiler tools, /usr/lib/emsdk/emsdk_env.sh or ~/.cpprc, ...

(Used in cross-compiler images)

EM_CACHE, EM_CONFIG, EMSCRIPTEN_PATH, EMSDK, EMSDK_PATH

Paths for emscripten, emsdk and configs.

  • EM_CACHE: /usr/lib/emsdk/upstream/emscripten/cache
  • EM_CONFIG: /usr/lib/emsdk/.emscripten
  • EMSCRIPTEN_PATH: /usr/lib/emscripten
  • EMSDK: /usr/lib/emsdk
  • EMSDK_PATH: /usr/lib/emsdk

(Used in emscripten images)

License

View license information for this image.

As with all Docker images, these likely also contain other software which may be under other licenses (such as Bash, etc from the base distribution, along with any direct or indirect dependencies of the primary software being contained).

As for any pre-built image usage, it is the image user's responsibility to ensure that any use of this image complies with any relevant licenses for all software contained within.

Links