Getting Started with CLion and CMake

LiveRunGrow
5 min readJan 12, 2021

--

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

CLion is an IDE for cross-platform development in C++ language. We can think of Clion to be similar to intellij in Java or Webstorm for Javascript programming.

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 is a tool used by Clion for development. CMake is used to control the software compilation process using (simple platform and compiler independent) configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

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

The last line, executable target, is an executable to be built using a CMake script.

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

Previously, to add executables target, we used add_executable(). If we want to add library targets, we need another command add_library(). As an example, let’s create a static library from the calc.cpp source file:

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

CS3203 Project Wiki Link

To understand more about cmake files:

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

For more on Coding with C++:

The End :)

--

--

LiveRunGrow

𓆉︎ 𝙳𝚛𝚎𝚊𝚖𝚎𝚛 🪴𝙲𝚛𝚎𝚊𝚝𝚘𝚛 👩‍💻𝚂𝚘𝚏𝚝𝚠𝚊𝚛𝚎 𝚎𝚗𝚐𝚒𝚗𝚎𝚎𝚛 ☻ I write & reflect weekly about software engineering, my life and books. Ŧ๏ɭɭ๏ฬ ๓є!