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
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
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 🙁
(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
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.
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.
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:
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.
Add external c++ libraries to a CLion project
I am using CLion from Mac, and i’m having problems to understand how can i add external libraries to my project. So, how can i add external libraries to a c++ project?
![]()
2 Answers 2
Manually edit CMakeLists.txt adding the following lines at the end with the proper paths for your system and proper ProjectName. This config is for an Ubuntu 17.04 workstation.
Hope this helps.
You can test it with the following:
in CMakeLists.txt , add external library information. first, you can define a logical name for the external library, say for e.g. we want to link a shared library which has .so file somewhere already installed on the system,
add_library(myLogicalExtLib SHARED IMPORTED)
IMPORTED means that the library already exists and we don’t need to build it here in this project.
then, we can supply the location information about this logical library as follows,
SFML 2.5.1 setup on macOS with CLion
I know that the title is very specific but that is the platform that I am doing some experiments with SFML.
So, if you want to create games with SFML and you have macOS and you want to use CLion as IDE just follow the next steps:
Install SFML
There are a lot ways to install SFML but the easiest is to install it with brew
With this command, you’re going to install the version 2.5.1.
Create the project with CLion
Now is time to create a new project with CLion, put the name that you want

Now, you need to update the CMakeList.txt to compile and link your project with the SFML libraries.
My recommendation is that you link the libraries that you are going to use, for example, only link and require sfml-graphics and sfml-audio for example.
A simple SFML project
Now , create a simple SFML code to check if all was installed correctly.
Run your project and you will see
Now you are ready to create games alt=»» /> !
Как установить библиотеку в clion
If you’re in the process of working on a project in C++, then you have probably recently made a bunch of decisions regarding which operating system to use, which IDE to use, and which compiler to use.
The issue is that there are very few IDE / build process options that can be used [almost] universally among all major operating systems.
The best option I have found is to use JetBrains CLion in conjunction with CMake to handle the build process. You will still need your own compiler, which may lead to some inconsistencies down the road, but CLion and CMake are the closest you will be able to get to a cross platform C++ solution.
Why CLion and CMake?
I spent a tremendous amount of time bouncing between different C++ IDEs because I could not find anything that satisfied my needs. Being able to develop my C++ projects on both my Windows PC and my iMac is very important to me. Therefore, operating system specific IDEs like Microsoft’s Visual Studio and Apple’s XCode were off the table immediately. After years of being unsatisfied with the available options, JetBrains CLion was released and all of my problems were immediately solved.
According to CLion’s website, " CLion project model is based on CMake — the cross-platform automatic build system. CMake itself doesn’t build a project but passes the build rules and targets listed in CMakeLists.txt file to a specific builder."
CMake is built right into CLion, which makes it the obvious tool for the job. As the quote above mentions, though, CMake does not actually compile the C++ code. You still need your own compiler to do that.
Since CLion and CMake both have cross-platform implementations, you can simply set up a C++ project in CLion, upload it to Github or another remote repository, and then pull it down on a different operating system and continue developing. This feature alone makes it more convenient and easy to use than Visual Studio, XCode, or even a text editor with GNU Make.
CMakeLists.txt
If you are familiar with Makefiles, then you know that dealing with targets and includes can be very messy and frustrating. CMake takes care of that through the use of a special file called CMakeLists.txt. This file can contain everything from compiler options, libraries to link to, include directories, source files, and much more.
Mini Tutorial 1: Creating a static library
I am going to walk you through the steps for creating a static library with CLion and CMake. In the next mini tutorial, we will be linking to this static library from a different project.
Note: You must have a compiler installed before starting this tutorial. On Windows, I use MinGW. On Mac, I use Clang.
Start by creating a new CLion project. Before you do anything else, navigate to the project’s folder you just created and drop the header(s) and implementation file(s) for the static library you want to create right next to main.cpp.

Go back into CLion and you should see the files you just added on the left side in the Project window.
It’s time to edit CMakeLists.txt to tell the compiler to create a static library out of our files. Even if you have never seen a CMakeLists.txt file before, you’ll notice that it isn’t nearly as cryptic as a Makefile. The instructions are real words that are very easy to understand. This is what the default instructions mean:

- cmake_minimum_required: The minimum required version of CMake that must be installed before compiling
- project: The name of the project. You can set this to anything you’d like
- set(CMAKE_CXX_FLAGS): Setting the options that will be sent to your compiler. You can put the version of C++, warning ignores, and more here
- set(SOURCE_FILES): All of the source files in your project. You do not need to put the header files. You can if you want to, but you are only required to put implementation files here
- add_executable: The command to create an executable. This command will actually be changed for creating a library instead of an executable
Let’s modify some of these instructions so that a static library will be created.
In the set(SOURCE_FILES) instruction, add your new library’s implementation file(s) after main.cpp. Then, change add_executable to add_library.
When you’re done, it should look something like this:

Make sure you reload the CMake project (you can do this by either clicking Enable Auto-Reload at the top-right of CLion when it gives you the option OR by right-clicking the CMakeLists.txt in the Project window on the left and selecting Reload CMake Project).
Then, compile the project. You should see something like this in the output window if it worked:

And that’s it! You can now navigate to the debug folder (mine’s in a very obscure place: C:\Users\Limeoats\.CLion2016.1\system\cmake\generated\tinyxml2-6d774863\6d774863\Debug) and grab the .a file that was generated.
Mini Tutorial 2: Linking to our new static library
Start off by creating a new folder somewhere where you will place your library files. Inside, create two new folders, lib and include.

Inside the lib folder, put the new .a file created in the previous mini tutorial.
Inside the include folder, put the header file(s) from the library.
Next, open up or create the project you want to include this library in. The first thing we need to do is create a cmake file that will look for our new library. You can technically hard-code the path to it in your CMakeLists.txt file, but creating a separate file to look for it is a better approach.
At the root of the project, create a new folder called cmake. Inside that folder, create a new file called Find[YourLibrary].cmake. My file will be called FindTinyXML2.cmake.

In the code below, you will want to replace tinyxml2 with whatever the name of your library is.