Getting Started with CLion and CMake

Image for post
Image for post

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.

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 :(

Image for post
Image for post
Error with running with CMake
Image for post
Image for post
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.

Image for post
Image for post

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:

Image for post
Image for post

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:

Image for post
Image for post

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.

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

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:

Image for post
Image for post

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 :)

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