Professional, zero-cost setup for C++ projects (Part 1 of N)

When talking about C++, the term zero-cost is often heard, like for example in the almost-buzzword “zero-cost abstraction”.
In this series of articles, we will take that common term (way too) literally, and attempt to apply it to the area of development environments rather than to the code.

In other words, our focus will be on setting up a professional development environment for open-source projects, for free. We will then cover all the aspects that are essential to us regarding the successful development of an open-source project:

  • Cross-platform compatibility (Windows/Linux/OSX)
  • Ability to manage dependencies easily
  • Code quality and stability guarantees
  • A good, up-to-date documentation
  • Easy deployment for the products
  • A project open to external contributions

The content of this post and the upcoming ones is based on our personal experience with C++ projects, and particularly on Nephtys, an open-source MMORPG in modern C++.
However, we won’t delve into the implementation of the project here, since what we present is merely a setup that you can adapt to any of your projects.

A toy project

Throughout the articles, we will showcase our setup and methods while building a tiny toy project, which will contain very little code, but will be organized like a real-world project. It will itself be divided into three parts: a client, a server, and a launcher.

Note: everything presented during this series of articles is of course a bit simplified, and can evolve and/or be improved depending on specific needs.

Without further ado, let us start !

Directory structure and cross-platform builds

Today, we are going to start with the basics: how the sources will be organized, and how the project will be built. In our case, we will use the CMake build generator, because it offers cross-platform support, is quite easy to learn, and thus often used for C++ projects.

For each of our subprojects, we will have a directory containing src, include, and test folders and a CMakeLists.txt file.
As you probably figured, the src folder will contain the source files, the include folder will contain the project headers, and finally the test folder will contain additional files required to write the unit tests of the project.

Here is how the CMakeLists.txt responsible for building the server would look like:

And now for the contents of the root CMakeLists.txt, performing common setup then delegating to each of the subprojects’:

That gives us the following directory tree:

At the moment, if you compile and run any of the programs in the project, you will get a hello world and a line break:

mkdir build
cd build/
cmake ..
--- The C compiler identification is GNU 8.2.0
--- The CXX compiler identification is GNU 8.2.0
--- Check for working C compiler: /usr/bin/cc
--- Check for working C compiler: /usr/bin/cc --- works
--- Detecting C compiler ABI info
--- Detecting C compiler ABI info -- done
--- Detecting C compile features
--- Detecting C compile features -- done
--- Check for working CXX compiler: /usr/bin/c++
--- Check for working CXX compiler: /usr/bin/c++ --- works
--- Detecting CXX compiler ABI info
--- Detecting CXX compiler ABI info -- done
--- Detecting CXX compile features
--- Detecting CXX compile features -- done
--- Configuring done
--- Generating done
--- Build files have been written to: /home/doom/stuff/c++/zero-cost-cpp-project/build
make
Scanning dependencies of target client
[ 16%] Building CXX object client/CMakeFiles/client.dir/src/main.cpp.o
[ 33%] Linking CXX executable client
[ 33%] Built target client
Scanning dependencies of target server
[ 50%] Building CXX object server/CMakeFiles/server.dir/src/main.cpp.o
[ 66%] Linking CXX executable server
[ 66%] Built target server
Scanning dependencies of target launcher
[ 83%] Building CXX object launcher/CMakeFiles/launcher.dir/src/main.cpp.o
[100%] Linking CXX executable launcher
[100%] Built target launcher
./client/client
Hello, World!

Conclusion

We now have a well-organized repository, and a way to compile our code on all three major platforms. It’s time for a commit !
Everything written during this article can be found on the corresponding GitHub repository.

That’s it for today ! Next time, we will talk about dependency management, and add fancy libraries to our project. Stay tuned !

Comments (2)

Leave a Reply

Your email address will not be published. Required fields are marked *