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

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

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

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:

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.