Getting Started with CLion and CMake

Hello, this is a simple post on getting started with CLion and CMake. I am taking a module in school which uses these tools. I thought i would share some findings i discovered along the way as i tried to set up my PC.

(1) CLion

Extra CLion Configurations for Windows

If your computer is on Windows, then you need to perform an additional configuration step on CLion to set up the environment: Cygwin, MinGW, WSL or Microsoft Visual C++ for adding the C++ Compilor.

Here, i choose to use Visual Studio (Purple Logo).

Previously, i selected MinGW but then the code had some problems. Hence, i switched to the Visual Studio instead.

Note: If you skip this section and you have a windows computer, then you might face issues running the next step with CMake. Screenshots below are the error messages i faced in CLion (this is after i started a new project file in CLion and tried to run CMakeLists.txt file in CLion) before installing Visual Studio :(

Error with running with CMake
I later found the cause of issue to be as seen here where the C++ Compiler was not detected.

(2) CMake

CMake uses scripts called CMakeLists to generate build files (makefiles) for a specific environment.

(3) Working with CMake in CLion

Check out the tutorial in the link above for a step by step tutorial on setting up a CMake project. The content in this section is referenced from the link.

When you create a new CMake project in CLion, a CMakeLists.txt file is automatically generated under the project root. Depending on the complexity of your project, you might have a CMakeLists.txt file for each folder. An example of a CMakeLists.txt file is as shown below.

Executable Target

In the above image, our test project has only one build target, cmake_testapp. Upon the first project loading, CLion automatically adds a Run/Debug configuration associated with this target:

Click Edit Configurations in the switcher or select Run | Edit Configurations from the main menu to view the details. The target name and the executable name were taken directly from the CMakeLists.txt:

Notice the Before launch area of this dialog: Build is set as a before launch step by default. So we can use this configuration not only to debug or run our target but also to perform the build.

If we want to add another file, titled calc.cpp, we need to update the CMakeLists.txt file.

When creating the new C++ File, remember to tick the boxes as seen above.

Library Targets

add_library(test_library STATIC calc.cpp)

As well as for executables, CLion adds a Run/Debug configuration for the library target after reloading the project:

However, this is a non-executable configuration, so if we attempt to run or debug it, we will get the Executable not specified error message.

To obtain the library file, we need to build the test_library target. For this, we can switch to the corresponding configuration and press the green build logo, or call Build | Build “test_library”. The libtest_library.a file will appear in the cmake-build-debug folder.

For more:

You might notice a project might have more than 1 CMakeLists.txt files.

As quoted from a project team mate, — there are many CMakeLists.txt files, but notice that there’s a tree order to these lists (e.g. top-level CMakeLists.txt indicates which files to compile, points to other ‘subdirectories’ which may have their own CMakeLists.txt) that cmake uses to build binaries. All the linking and compiling are done by cmake, and the binary formed is stored in /cmake-build-debug

Others

To understand more about cmake files:

http://www.elpauer.org/stuff/learning_cmake.pdf

For more on Coding with C++:

The End :)

This is a repository of my thoughts on my personal life, my random interests & notes taken down as I navigate my way through the tech world!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store