Skip to main content

C and C++

This guide provides tips and hints for building C and C++ projects.

Overview

GNU C and C++ compilers are pre-installed in the Semaphore environment. You can switch the active compiler using sem-version on Linux machines.

How to compile C

The Ubuntu Linux machines ship with the GNU C compiler.

  1. Open the pipeline

  2. Select one of the Linux Ubuntu machines

  3. Use the following commands in the job

    checkout
    gcc --version
    gcc hello.c -o hello
    ./hello

    Compiling C program

  4. Press Run the workflow

See artifacts to learn how to save and persist the built binary.

How to change gcc version

Use sem-version to switch between gcc compiler versions on Linux. Changing gcc compilers on macOS is not supported.

# use gcc 10.5
sem-version c 10
$ gcc --version
gcc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0

# use gcc 9.4
sem-version c 9
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0

Using Docker containers

The sem-version tool does not work on Docker containers. If the compiler you need is not shipped in the Semaphore Linux or macOS image, you must build a Docker image and run jobs using Docker environments.

Semaphore does not provide pre-built C and C++ Docker images. Find Dockerfiles to build your custom images in the semaphoreci/docker-images repository.

How to compile C++

The Ubuntu Linux machines ship with the GNU C++ compiler.

  1. Open the pipeline

  2. Select one of the Linux Ubuntu machines

  3. Use the following commands in the job

    checkout
    g++ --version
    g++ hello.cc -o hello
    ./hello

    Compiling C++ program

  4. Press Run the workflow

See artifacts to learn how to save and persist the built binary.

How to change the g++ version

Use sem-version to switch between g++ compiler versions on Linux. Changing gcc compilers on macOS is not supported..

# use gcc 10.5
sem-version cpp 10
$ g++ --version
g++ (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0

# use gcc 9.4
sem-version cpp 9
$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0