Skip to main content

C/C++

You can build and test C and C++ applications on Harness Cloud or a self-managed Kubernetes cluster build infrastructure. This guide uses a Linux platform, but you can also use macOS and Windows platforms with Harness.

This guide assumes you've created a Harness CI pipeline.

Install dependencies

Use Run steps to install dependencies in the build environment.

You can run any commands in Run steps as long as the necessary binaries are available on the host machine or the referenced container image. For example, you can run cURL commands, build commands for CMake, Ninja, or MSBuild, or any other commands you might otherwise run on the command line.

In the following YAML example, the Run step runs a Python script to get dependencies for a C++ project. It uses a matrix looping strategy to cycle through each dependency.

- step:
type: Run
name: Fetch Deps
identifier: Fetch_Deps
spec:
shell: Sh
command: python3 build/getdeps.py fetch --no-tests <+matrix.deps>
failureStrategies: []
strategy:
matrix:
deps:
- ninja
- cmake
- zlib
- zstd
- boost
- double-conversion
- fmt
- gflags
- glog
- googletest
- libevent
maxConcurrency: 1
tip

In addition to Run steps, Plugin steps are also useful for installing dependencies.

You can use Background steps to run dependent services that are needed by multiple steps in the same stage.

Cache dependencies

Cache your C and C++ dependencies with Cache Intelligence. Add caching.enabled.true to your stage.spec.

- stage:
spec:
caching:
enabled: true

Build and run tests

You can use Run steps to run tests in CI pipelines.

- step:
type: Run
name: build and test
identifier: build and test
spec:
shell: Sh
command: |-
cmake -S . -B build
ctest --test-dir $FILES --output-junit /target/reports/test_output.xml

Visualize test results

If you want to view test results in Harness, make sure your test commands produce reports in JUnit XML format and that your steps include the reports specification.

reports:
type: JUnit
spec:
paths:
- target/reports/*.xml

Test splitting

Harness CI supports test splitting (parallelism) in Run steps.

Specify version

CLang and GNU C++ are pre-installed on Harness Cloud runners. For details about all available tools and versions, go to Platforms and image specifications.

If you want to use a different compiler or a specific version of a compiler, you can use a Run step to install it, for example:

- step:
type: Run
name: install
identifier: install
spec:
shell: Sh
command: |
sudo apt update
sudo apt install g++-12

Full pipeline examples

Here's a YAML example of a pipeline that builds a C application and pushes the image to Docker Hub.

This pipeline uses Harness Cloud build infrastructure and Cache Intelligence.

Pipeline YAML
pipeline:
name: Build C
identifier: Build_C
projectIdentifier: default
orgIdentifier: default
properties:
ci:
codebase:
connectorRef: YOUR_CODE_REPO_CONNECTOR_ID
repoName: YOUR_REPO_NAME
build: <+input>
stages:
- stage:
name: Build
identifier: Build
description: ""
type: CI
spec:
caching:
enabled: true
cloneCodebase: true
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}
execution:
steps:
- step:
type: Run
name: Run_1
identifier: Run_1
spec:
shell: Bash
command: |-
chmod 777 ./scripts/docker/build.sh
./scripts/docker/build.sh --bionic --remote PROJECT test_PROJECT
- step:
type: BuildAndPushDockerRegistry
name: BuildAndPushDockerRegistry_1
identifier: BuildAndPushDockerRegistry_1
spec:
connectorRef: YOUR_DOCKER_CONNECTOR_ID
repo: YOUR_DOCKER_HUB_USERNAME/DOCKER_REPO_NAME
tags:
- <+pipeline.sequenceId>

Next steps

Now that you have created a pipeline that builds and tests a C or C++ app, you could: