Difference between CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_LIST_DIR

The variables CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_LIST_DIR may refer to different directories for a CMake list file that is included by another file with the include command. E.g., if a CMakeLists.txt is present in a directory project and contains the following directive include(src/CMakeLists.txt) then while src/CMakeLists.txt is being processed CMAKE_CURRENT_LIST_DIR will refer to project/src whereas CMAKE_CURRENT_SOURCE_DIR still … Read more

Cause CMAKE to generate an error

The message() method has an optional argument for the mode, allowing STATUS, WARNING, AUTHOR_WARNING, SEND_ERROR, and FATAL_ERROR. STATUS messages go to stdout. Every other mode of message, including none, goes to stderr. You want SEND_ERROR if you want to output an error, but continue processing. You want FATAL_ERROR if you want to exit CMake processing. … 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

Creating a directory in CMake

When do you want to create the directory? At build system generation To create a directory when CMake generates the build system, file(MAKE_DIRECTORY ${directory}) At build time In the add_custom_command() command (which adds a custom build rule to the generated build system), and the add_custom_target() command (which adds a target with no output so it … Read more

Is it better to specify source files with GLOB or each file individually in CMake?

Full disclosure: I originally preferred the globbing approach for its simplicity, but over the years I have come to recognise that explicitly listing the files is less error-prone for large, multi-developer projects. Original answer: The advantages to globbing are: It’s easy to add new files as they are only listed in one place: on disk. … Read more

What use is find_package() when you need to specify CMAKE_MODULE_PATH?

Command find_package has two modes: Module mode and Config mode. You are trying to use Module mode when you actually need Config mode. Module mode Find<package>.cmake file located within your project. Something like this: CMakeLists.txt cmake/FindFoo.cmake cmake/FindBoo.cmake CMakeLists.txt content: list(APPEND CMAKE_MODULE_PATH “${CMAKE_CURRENT_LIST_DIR}/cmake”) find_package(Foo REQUIRED) # FOO_INCLUDE_DIR, FOO_LIBRARIES find_package(Boo REQUIRED) # BOO_INCLUDE_DIR, BOO_LIBRARIES include_directories(“${FOO_INCLUDE_DIR}”) include_directories(“${BOO_INCLUDE_DIR}”) add_executable(Bar … Read more

How exactly does CMake work?

The secret is that you don’t have to understand what the generated files do. CMake introduces a lot of complexity into the build system, most of which only pays off if you use it for building complex software projects. The good news is that CMake does a good job of keeping a lot of this … 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

CMake: Print out all accessible variables in a script

Using the get_cmake_property function, the following loop will print out all CMake variables defined and their values: get_cmake_property(_variableNames VARIABLES) list (SORT _variableNames) foreach (_variableName ${_variableNames}) message(STATUS “${_variableName}=${${_variableName}}”) endforeach() This can also be embedded in a convenience function which can optionally use a regular expression to print only a subset of variables with matching names function(dump_cmake_variables) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)