C++ Build Process

I remember my time at the university, when working on C++ code, all of the command line commands I executed to get the my to compile and link. For the small projects I did back then, that was more than enough. We did get some small introduction into make, but we didn’t go into detail about how it all worked. When we started to work with Java, we had to use Maven, and again we didn’t get any real introduction. In my short career I have discovered the world of Maven and I am now more aware of how it all works, making me proficient enough to do what I need to manage bigger projects.

My love for C++ however never really faded, and I have been looking into doing some more C++ coding again. To get me started I was investigating build process tools for C++, in my search I found a couple of solutions: CMake, Maven and Gradle. The goal I set out for all of these tools, was to get a simple project and some tests for it to build.

The reason I selected CMake and not Make, was because I would prefer to have cross-platform supported out of the box. With Make this would become more difficult, if not impossible. CMake was very easy to get working on a simple project and only requires a couple of lines. At the same time, you can get into very complex configurations, as it leaves you with full control over everything. As I have been using Maven for the last few years, I was a bit shocked to see that I had to list all of my source files myself. While there is a way to bypass this, by including the whole directory this is not advised. Creating tests was just as easy as it is also just creating an executable that contains all of your tests to run.

Maven, the tool I have gotten used to in the past few years did of course do automatic source code detection and all of those fancy things. But Maven was designed for Java, luckily there is a nar plugin to build C++. For some unknown reason my Maven was not able to get this extension, as it always failed with the same error:

Unresolveable build extension: Plugin org.apache.maven.plugins:maven-nar-plugin:2.1-SNAPSHOT
Unknown packaging: nar

Because Maven does not officially support C++, I just gave up on it and went on to Gradle.

Gradle, in contrary to Maven, does officially support C++ and even has guides for it on the official website. Because of this it was very easy to get my application working. Gradle, just like Maven assumes that files are located in a certain folder hierarchy. As long as you stick to this, it is very easy to get everything working and requires almost no work at all. Creating tests however was a lot more hassle, as it was complaining about multiple main methods: one for the real application and another for the tests. The only solution I found was to only build either the real application or the tests, but never together.

Libraries are handled almost the same by both CMake and Gradle. None of them delivers the easy dependency management Maven offers for Java, but then again this is only possible because of the maven repository consisting of jar files. Because C++ is platform-dependent this approach is nearly impossible. For neither of them is it very hard to include a library file into the build. But to get this working on multiple platforms, you actually require multiple versions of the library and link with the correct one.

An advantage of CMake however is that it is still widely used by other projects. This makes adding a dependency easier, as you can make building that dependency part of your own build process. In my opinion this is a very big advantage of CMake and is the reason why I have decided to use CMake for my own projects.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s