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.
Extra CLion Configurations for Windows
Tutorial: Configure CLion on Windows - Help | CLion
On Windows, configuring CLion requires setting up the environment: Cygwin, MinGW, WSL, or Microsoft Visual C++. You can…
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 :(
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
Quick CMake tutorial - Help | CLion
This tutorial will guide you through the process of creating and developing a simple CMake project. Step by step, we…
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.
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.
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.
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
CS3203 Project Wiki Link
The Cross-platform Startup SPA Solution with Cmake offers a starting point for your project development and testing. In…
To understand more about cmake files:
CMake Tutorial - CMake 3.19.2 Documentation
The CMake tutorial provides a step-by-step guide that covers common build system issues that CMake helps address…
For more on Coding with C++:
C++ Core Guidelines
August 3, 2020 Editors: This is a living document under continuous improvement. Had it been an open-source (code)…