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

How do you add Boost libraries in CMakeLists.txt?

Put this in your CMakeLists.txt file (change any options from OFF to ON if you want): set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) find_package(Boost 1.45.0 COMPONENTS *boost libraries here*) if(Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) add_executable(progname file1.cxx file2.cxx) target_link_libraries(progname ${Boost_LIBRARIES}) endif() Obviously you need to put the libraries you want where I put *boost libraries here*. For example, if you’re … Read more

Define preprocessor macro through CMake?

For a long time, CMake had the add_definitions command for this purpose. However, recently the command has been superseded by a more fine grained approach (separate commands for compile definitions, include directories, and compiler options). An example using the new add_compile_definitions: add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION}) add_compile_definitions(WITH_OPENCV2) Or: add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2) The good part about this is that it circumvents … Read more

How to link C++ program with Boost using CMake

In CMake you could use find_package to find libraries you need. There usually is a FindBoost.cmake along with your CMake installation. As far as I remember, it will be installed to /usr/share/cmake/Modules/ along with other find-scripts for common libraries. You could just check the documentation in that file for more information about how it works. … Read more

Cmake cannot find library using “link_directories”

Do not use link_directories like this in CMake. This is a common beginner’s mistake, as many other build environments work like this, but in CMake it’s just asking for trouble. Even the manpage specifically advises against it: Note that this command [link_directories] is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. … Read more

CMAKE_BUILD_TYPE is not being used in CMakeLists.txt

There are two types of generators: single-configurations and multi-configurations. Single configurations Make-like generators: Unix Makefiles, NMake Makefiles, MinGW Makefiles, … You set the configuration type in the generate step: cmake -H. -B_builds/Debug -DCMAKE_BUILD_TYPE=Debug “-GUnix Makefiles” In this case, the build step is always Debug: > cmake –build _builds/Debug /usr/bin/c++ -g … > cmake –build _builds/Debug … 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

How do I make CMake output into a ‘bin’ dir?

As in Oleg’s answer, I believe the correct variable to set is CMAKE_RUNTIME_OUTPUT_DIRECTORY. We use the following in our root CMakeLists.txt: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) You can also specify the output directories on a per-target basis: set_target_properties( targets… PROPERTIES ARCHIVE_OUTPUT_DIRECTORY “${CMAKE_BINARY_DIR}/lib” LIBRARY_OUTPUT_DIRECTORY “${CMAKE_BINARY_DIR}/lib” RUNTIME_OUTPUT_DIRECTORY “${CMAKE_BINARY_DIR}/bin” ) In both cases you can append _[CONFIG] … Read more