My God, It’s Full Of Stars: And then there was CMake

Welcome back at the 2nd part of the n-body-problem series. With the first post we had a short view on the equations of motion we need to calculate the position in space of a point mass. This time we need to conclude our equations and create a minimum project setup. I would like to use CMake for project setup for two reasons.

  1. I would like to keep the project environment and IDE independent.
  2. I’m new to CMake and want to learn it since a couple of years

Let’s start and get back into our project. First of all we are still missing the equation to calculate the change of velocity a mass point is experiencing under acceleration.

v=v_{0}+at

Now we can start with our project setup. As a source of information how to use CMake I use the CMake documentation and a GitBook. First of all we define a CMake typical folder structure:

Both folders, root and solver must contain a CMakeLists.txt (1)(4). The files solver.h/.cpp are containing only a dummy HelloWorld class to test and demonstrate the project structure and test (5) setup. The cmake folder provides all necessary Find*.cmake files we need to derive for our project all necessary external libraries.

Lets go through the different files in our basic project setup starting with root CMakeLists.txt.

Line 1-5 is defining, according to modern-cmake, the range a CMake installation must fulfill for building the project. If the version of CMake is below version 3.12, cmake_policy is preserving backwards compatibility. Line 10-20 is adding the cmake folder to the module path needed to resolve external dependencies via find_package and Find*.cmake files. In case Catch2 can’t be found, it’s possible to download the testing library via -DDOWNLOAD_CATCH=1 parameter defined in FindCatch.cmake. In Line 22-25 we add the external Catch2 testing library with add_library and the parameter INTERFACE which ist telling CMake that the library is a pure header interface without any .lib or .a file. With target_include_directory we set where to find the Catch2 header. At the last two lines we are including CTest, which we can invoke after building the project to run our tests, and adding the subdirectory of our solver to the project. With add_subdirectory CMake knows where to search for additional CMakeLists.txt files.

Now let’s have a look at the solver’s CMakeLists.txt

First of all we have to tell CMake how our library is called, which type we want to have it and where it’s necessary implementation files are. In line 1-3 we see how this is done via add_library. The library needs also its headers which we include through target_include_directories and additional export them and its necessary header via the PUBLIC parameter. PUBLIC we need to use because we are also building a .lib or .a file which holds the implementation. Here we might need to extend the CMakeLists.txt later when we really exporting the library.

Now we have our minimal project setup which we can extend if it gets necessary. Additional we have a dummy library and a test project which is loading and executing the library. You can get the project at this state via GitHub. At line 8-12 we add the executable generated by Catch2 for running our solver specific tests. With add_executable we add the file which contains the main function, and all .cpp files which contain our tests. With target_link_libraries we link the solver library onto the test executable. The last two lines are enabling CTest and registering the test executable to tell CTest what to execute. You can build the project and run its test via the following commands:

Summary

So that’s it for today’s post. We successfully setup a very small and basic project from where we can start to implement our n-body-problem project. If necessary I think it will be rather easy to extend it. Maybe we add later a Qt based UI to show us our calculation results. The next post we start to implement the solver and a couple of tests which we need to validate our solver is working and evolving the right way.

Did you like this post?

What are your thoughts on this post?

Feel free to comment and share the post.

Processing…
Success! You're on the list.

4 thoughts on “My God, It’s Full Of Stars: And then there was CMake

    1. Yes we use mostly also VS2017. Only for small parts which need to be crosscompiled are done with CMake. But I always wanted to learn it, so this is a good opportunity. Also gradle is very nice, but in an early state regarding c++, I used it mostly for java development.

      Like

Leave a Reply to Martin Cancel 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.