Introduction into C++ Builds with Gradle
Tags: codeproject, programming, buildautomation, cpp
Published: 2019-05-17 12:12:26

---
title: Introduction into C++ Builds with Gradle
published: true
tags: CodeProject,programming,Build Automation,CPP
canonical_url: https://thoughts-on-cpp.com/2019/04/10/introduction-into-c-builds-with-gradle/
---
Posted on [thoughts-on-cpp.com](https://thoughts-on-cpp.com)

Welcome back to a new post on thoughts-on-cpp.com. In today’s post, I would like to give an introduction to the build system [Gradle](https://gradle.org) and how we can use it to build native applications and libraries. Gradle is originally coming from the Java world, but it’s also supporting native build toolchains for quite a while. Gradle is becoming more and more popular in the Java world and is on a good way to rule out the old bull [Maven](https://maven.apache.org/). This is because of two features which we can also benefit from in the native (C/C++, Objective-C/C++, Assembly, and Windows resources) build world. These features are Gradle’s easy to maintain and very expressive Groovy (or Kotlin if preferred) based [DSL](https://en.wikipedia.org/wiki/Domain-specific_language), and it’s capabilities of dependency resolving via online and on-premise library providers (such as maven-central, Artifactory, Bintray, etc.) or local repositories.

Let’s start with a [multi-project example](https://github.com/Ben1980/gradleNativ) we already know from my post [Introduction into an Automated C++ Build Setup with Jenkins and CMake](https://dev.to/ben1980/introduction-into-an-automated-c-build-setup-with-jenkins-and-cmake-do-temp-slug-5798207). I have just slightly changed the example hello world application. This time the main function is printing out “Hello World!” onto console using a shared library, called greeter. The Greeter class itself is utilizing the external library [{fmt}](http://fmtlib.net/latest/index.html) to print “Hello World” onto the screen. If you’ve wondered about the Gradle directory and files, those are provided by the Gradle wrapper which facilitates us to build the project without even installing Gradle upfront.

{% gist https://gist.github.com/Ben1980/f5e12820dbd69382cc1f6bc969dc3949 %}

The Gradle native build plugin is quite straight forward to configure. Every Gradle project needs a build.gradle file at its root directory as an entry point and one at each subproject. In most cases, we will do general configurations in a build.gradle file located at the root directory. But there is no need, it can also be empty. By default, Gradle is looking for sources in the directory `src/main/cpp`. For libraries, public headers are defined in `src/main/public` and in case they should be used only library internal (private) the default directory is `src/main/headers`. In case we define the headers also in `src/main/cpp`, the headers are treated as private as well. If we like to overwrite the default source directories we just need to define them according to this [example](https://github.com/gradle/native-samples/blob/master/cpp/swift-package-manager/build.gradle#L9-L17). To be able to resolve dependencies between subprojects, we need to define a settings.gradle file which is including our subprojects `include 'app', 'greeter', 'testLib'`.

```
components.withType(ProductionCppComponent) { 
//By convention, source files are located in the root directory/Sources/
source.from rootProject.file("Sources/${subproject.name.capitalize()}") 
privateHeaders.from rootProject.file("Sources/${subproject.name.capitalize()}/include") 
} 
components.withType(CppLibrary) { 
//By convention, public header files are located in the root directory/Sources//include 
publicHeaders.from rootProject.file("Sources/${subproject.name.capitalize()}/include") 
}
```

As build definition of our root directory we just simply define IDE support for each subproject. [CLion](https://www.jetbrains.com/clion/), for example, has native Gradle support, so importing Gradle projects works smooth as silk. Therefore our root build.gradle file looks like the following.

```
allprojects { 
apply plugin: 'xcode' 
apply plugin: 'visual-studio' 
}
```

The application build configuration is defined at the app directory starting with calling the [`cpp-application`](https://docs.gradle.org/current/javadoc/org/gradle/language/cpp/CppApplication.html) plugin which is generating an executable file which can be found and executed at `app/build/install/main/{buildType}/{machine}`. Project internal dependencies can be defined by the `dependencies` clause with the implementation of the dependency defined as a project and the given name of the dependency. By default, Gradle is assuming the current host as target machine. If we want to consider other target machines we have to declare them as we do in our example with the [`targetMachines`](https://docs.gradle.org/current/javadoc/org/gradle/language/ComponentWithTargetMachines.html#getTargetMachines--) statement.

{% gist https://gist.github.com/Ben1980/e3c705686c883419cb5919bca2f5f003 %}

The library build configuration is defined at the greeter directory starting with calling the [`cpp-library`](https://docs.gradle.org/current/javadoc/org/gradle/language/cpp/CppLibrary.html) plugin and the type of [linkage](https://docs.gradle.org/current/javadoc/org/gradle/nativeplatform/Linkage.html), which can be STATIC and SHARED. Gradle is assuming we want SHARED libraries as default. A bit special is the way of how we have to resolve the dependency to the header only library {fmt}. Unfortunately, Gradle is not supporting header only libraries out of the box, but we can accomplish a workaround by adding the include path to the `includePathConfiguration` of the resulting binary. All other dependencies can be defined as `api`, in case we want to share the external dependency api with all consumers of our own defined library, or `implementation` in case we only want to use the dependency api private with our own library. A good example can be found in Gradle’s [example repository](https://github.com/gradle/native-samples).

{% gist https://gist.github.com/Ben1980/6f15404765bab84c3e38a262dbcbf4ee %}

With Gradle, we can not only build applications and libraries, but we can also execute tests to check the resulting artifacts. A test can be defined by the `cpp-unit-test` plugin which is generating a test executable. In principle, we could use any of the existing big test libraries, such as [googletest](https://github.com/google/googletest), but in my opinion, the out of the box solution is pretty neat and lightweight and can be extended quite easily with external libraries.

{% gist https://gist.github.com/Ben1980/9dbc185afd8d9eea2337b54d192e4313 %}

With this project setup, we can build all artifacts by the command `./gradlew assemble` and run tests by `./gradlew check`. If we want to build and run all tests together we can invoke `./gradlew build`. In case we need a list of all available tasks provided by Gradle and its plugins we can simply list them including their description by the command `./gradlew tasks`. At [GitHub](https://github.com/Ben1980/gradleNativ) you can find the resulting repository.

{% gist https://gist.github.com/Ben1980/fcc534bee9eba6cffa0a4b268f172a9b %}

![gradlebuild](https://thoughtsoncpp.files.wordpress.com/2019/04/gradlebuild.gif?w=800)

UPDATE: Thanks to [D. Lacase](https://twitter.com/lacasseio) I updated the source set configuration section to customize the folder structure of a project.

Did you like the post?

What are your thoughts?

Feel free to comment and share this post.