How to use CMake on Linux
Environment
- Linux shell (Ubuntu 18.04)
- Visual Studio Code
Understanding The Basics
- makefile
- CMakeLists.txt
The concept of makefile
To illustrate what makefile
does, we first create a project folder (I named it CMakeTuto
here).
Then use VSCode to open the folder.
1 | mkdir CMakeTuto |
Create a file makefile
1 | default: |
Note here makefile do not accept spaces. It only accept tabs.
Create a file main.cpp
1 |
|
Now your CMakeTuto
directory should be like this:
1 | . |
So what a makefile does?
Use the make
command to run the makefile
1 | make |
make
command basically run the command inmakefile
. (that isg++ main.cpp -o out
in this case. )
Now a out
file should appear. Run the out
file.
1 | ./out |
It should print the “Hello World!”.
The concept of CMake
CMake makes your makefile.
- cmake relies on a top level file called
CMakeLists.txt
now we can first delete our makefile
and out
in the previous section.
First check our cmake version
1 | cmake --version |
You can also use the command
cmake
to check the usage.Common usage:
cmake [options] <path-to-source>
Create a file CMakeLists.txt
, leave the file blank.
Now your directory should be:
1 | . |
Lets create a directory build.
1 | mkdir build |
Note if you want to use cmake
command, you need to go to the build directory (which is build
).
1 | cd build |
..
means upper level folder (which isCMakeTuto
in this case).
means this folder
Now your directory should be like this:
1 | . |
Notice now the
Makefile
is generated.If You look into that
Makefile
and you will see it just build what ourCMakeLists.txt
require.(Note we haven’t write anything into
CMakeLists.txt
yet)We won’t teach those generated files.
Edit your CMakeLists.txt
Now write into your CMakeLists.txt
.
1 | cmake_minimum_required(VERSION 3.9.0) |
run the cmake command in your build folder again.
1 | cmake .. |
If You look into that
Makefile
again and you will see it just build what ourCMakeLists.txt
require.
Lets run the Makefile
using make
command.
1 | make |
You should see this outcome:
1 | Scanning dependencies of target HelloWorld |
And now you should see a HelloWorld
executable is in the build folder.
Lets run the HelloWorld
executable.
1 | ./HelloWorld |
Now you know the basics of cmake.
Another Example - Linking more than 1 file
File structure:
1 | . |
adder.cpp
1 | float add(float a, float b) |
CMakeLists.txt
1 | cmake_minimum_required(VERSION 3.9.0) |
main.cpp
1 | //function prototype |
Build and run the program
1 | mkdir build |
It should print out
20.1
.
Libraries and Subdirectories
Levels and Libraries
In practical situations we often put our files into a folder.
We need to put a sublevel CMakeLists.txt
in each subfolder.
Example:
1 | . |
adder.cpp
1 |
|
adder.h
1 | float add(float a, float b); |
main.cpp
1 |
|
the root CMakeLists.txt
1 | cmake_minimum_required(VERSION 3.9.0) |
the sublevel Adder/CMakeLists.txt
1 | add_library(adder adder.cpp) #build as a library in the most simplistic way possible |
Build and run the program
1 | mkdir build && cd build |
It should print out
20.1
.