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.
- Editor
- YAML
-
Open the pipeline
-
Select one of the Linux Ubuntu machines
-
Use the following commands in the job
checkout
gcc --version
gcc hello.c -o hello
./hello -
Press Run the workflow
- Open the pipeline file
- Select one of the Linux Ubuntu agents
- Add a job to compile the source code with gcc
- Save and push the pipeline file to the repository
version: v1.0
name: Initial Pipeline
agent:
machine:
type: e1-standard-2
os_image: ubuntu2004
blocks:
- name: Compile
dependencies: []
task:
jobs:
- name: gcc
commands:
- checkout
- gcc --version
- gcc hello.c -o hello
- ./hello
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.
- Editor
- YAML
-
Open the pipeline
-
Select one of the Linux Ubuntu machines
-
Use the following commands in the job
checkout
g++ --version
g++ hello.cc -o hello
./hello -
Press Run the workflow
- Open the pipeline file
- Select one of the Linux Ubuntu agents
- Add a job to compile the source code with gcc
- Save and push the pipeline file to the repository
version: v1.0
name: Initial Pipeline
agent:
machine:
type: e1-standard-2
os_image: ubuntu2004
blocks:
- name: Compile
dependencies: []
task:
jobs:
- name: g++
commands:
- checkout
- g++ --version
- g++ hello.cc -o hello
- ./hello
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