Several cmake tips

There are some basic useage about the cmake on the youtube videos here.

Here are some other tips regarding the cmake. Which are important but not obvious in all kinds of tutorials.

CMake cache preloading

Some recently known parameter.
About the -C parameter. This parameter can help to load a cmake file before executing the actual cmake operation. The project path can be listed based on this -C parameter. For example, the project path might be different in the local env and remote env for same depedent library, we can store two .cmake files. One is for the local env and another is for the remote env. When we execute the cmake, the command becomes short, we do not need to maintain a long command line for each cmake command.

We ususlly put multiple set (<VAR_NAME> <VALUE>) into associated cmake file.

For example, this is the .cmake file that we want to load during the initilization stage (It is necessary to use the CACHE to tell cmake that these variables will be cached, and the parameter after the CACHE represents the type of the parameter):

// env.cmake file
set (VAR1 "path1/path2" CACHE STRING "description")
set (VAR2 "path2/path3" CACHE STRING "description")
set (VAR3 "path3/path4" CACHE STRING "description")

Then at the beginning of the current CMakeLists.txt file, we add these messages to print out the variable:

message (STATUS ${VAR1})
message (STATUS ${VAR2})
message (STATUS ${VAR3})

When we executing the cmake command, we can do sth like:

$ cmake -C ../env.cmake ..
loading initial cache file ../env.cmake
-- path1/path2
-- path2/path3
-- path3/path4
static off
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zw/cworkspace/src/5MCST/cmake_example/basic/build

By using -C to load the cmake configuration file, we can get the assocaited variables.

If we check the variable listed in CMakeCache file, we can see that:

$ cat CMakeCache.txt |grep VAR

The associated variables are recorded into the CMakeCache file.

Set verbose

Actually, the cmake is just the mapping from the cmake expression into the makefile. One typical expression is that the cmake VERBOS=1 which will print out all kinds of make commands. It is really helpful for solving the compiling issue to see what are the root reason that causes the compiling error. This is definately one frequent command that I adopted for solving the compiling issue.

Prefix path

We always use the -D<Library>_Path to specify the installed library. Another way is to set the CMAKE_PREFIX_PATH, this command set the path to find the installed libaray. If we assume the library need to be detected by pkgconfig, we need to use this one. For example


By this way, even if we do not set the -D<Library>_Path, the cmake find command can also find the approporite libraries accordingly. (Maybe for some cases, the find package in CMakeLists is not set properly, we can then use this variable)

Cuda compiling

One importnat thing it to remember that compilling with cuda can use the nvcc, then linking with the compiled c file need to use the gcc.

Although the cmake related command is easy to do, such as this one, it is good to have some ideas about how make levels thing to this work.

This is an example to compile and link cuda object on hpc.