How to specify a compiler in CMake?

To select a specific compiler, you have several solutions, as exaplained in CMake wiki: Method 1: use environment variables For C and C++, set the CC and CXX environment variables. This method is not guaranteed to work for all generators. (Specifically, if you are trying to set Xcode’s GCC_VERSION, this method confuses Xcode.) For example: … Read more

CMake + GoogleTest

This is an unusual case; most projects specify install rules. CMake’s ExternalProject_Add module is maybe the best tool for this job. This allows you to download, configure and build gtest from within your project, and then link to the gtest libraries. I’ve tested the following CMakeLists.txt on Windows with Visual Studio 10 and 11, and … Read more

CMake: In which order are files parsed (cache, toolchain, etc.)?

There’s no official documentation about this particular inner workings of CMake, so please find below a summary of what I’ve learned about CMake so far … What files are parsed depends on the The host and target operating system The target compiler Your host computer’s environment (variables, registry, installed software) Your project’s CMake script files, … Read more

CMake does not find includes / libraries

CMake’s find routines look for headers and libraries at some specific places. This includes the PATH variable, and the default locations for installed software, e.g., for many Linuces /usr/bin. Additionally, it evaluates the CMake variable CMAKE_PREFIX_PATH. You have two possibilities to help CMake finding the required files: Check whether your software is properly installed. For … Read more

Using CMake with GNU Make: How can I see the exact commands?

When you run make, add VERBOSE=1 to see the full command output. For example: cmake . make VERBOSE=1 Or you can add -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON to the cmake command for permanent verbose command output from the generated Makefiles. cmake -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON . make To reduce some possibly less-interesting output you might like to use the following options. The … Read more

What is the proper way to use `pkg-config` from `cmake`?

First of, the call: include(FindPkgConfig) should be replaced with: find_package(PkgConfig) The find_package() call is more flexible and allows options such as REQUIRED, that do things automatically that one would have to do manually with include(). Secondly, manually calling pkg-config should be avoid when possible. CMake comes with a rich set of package definitions, found in … Read more

What’s the CMake syntax to set and use variables?

When writing CMake scripts there is a lot you need to know about the syntax and how to use variables in CMake. The Syntax Strings using set(): set(MyString “Some Text”) set(MyStringWithVar “Some other Text: ${MyString}”) set(MyStringWithQuot “Some quote: \”${MyStringWithVar}\””) Or with string(): string(APPEND MyStringWithContent ” ${MyString}”) Lists using set(): set(MyList “a” “b” “c”) set(MyList ${MyList} … Read more

Hinting Find.cmake Files with a custom directory

Setting CMAKE_PREFIX_PATH variable serves exactly these purposes: hinting find_* function about location of searched item. While description of this variable doesn’t note about find_package function, the variable affects it indirectly: the most of Find<name>.cmake scripts use find_library and find_path functions. Note, that all find_* functions have precise algorithm for search items, and paths constructed with … Read more

CMake link to external library

arrowdodger’s answer is correct and preferred on many occasions. I would simply like to add an alternative to his answer: You could add an “imported” library target, instead of a link-directory. Something like: # Your-external “mylib”, add GLOBAL if the imported library is located in directories above the current. add_library( mylib SHARED IMPORTED ) # … Read more