This is an old revision of the document!
Table of Contents
Programming - Build Systems - cmake - Notes
Using
If you see a CMakeLists.txt in the source tree, and there is no documentation on how to compile, the following ususally works relative to the directory with the CMakeLists.txt:
mkdir build cd build cmake ../
Adding include dirs when calling cmake
If cmake does not find certain include dirs on it's own, and you don't want to start editing cmake config files, you can expand the list of directories to search for include files when running cmake.
cmake -DCMAKE_CXX_FLAGS=-isystem\ /path/to/my/include <path to my sources>
Source:stack overflow - Adding include directories to CMake when calling it from the command line
Adding both C++ and C includes:
cmake -DCMAKE_C_FLAGS=-isystem\ /usr/X11R6/include -DCMAKE_CXX_FLAGS=-isystem\ /usr/local/include ../
Adding linker flags when calling cmake
For libraries: CMAKE_SHARED_LINKER_FLAGS
For executables: CMAKE_EXE_LINKER_FLAGS
cmake -DCMAKE_SHARED_LINKER_FLAGS="-fstack-protector" -DCMAKE_EXE_LINKER_FLAGS="-fstack-protector" /path/to/source
Source: stack overlfow - How can I add linker flag for libraries with CMake?
Adding /usr/local/lib/ to the search path for the linker:
cmake -DCMAKE_SHARED_LINKER_FLAGS="-L/usr/local/lib" -DCMAKE_EXE_LINKER_FLAGS="-L/usr/local/lib" /path/to/source
Adding compiler flags when calling cmake
cmake -DCMAKE_C_FLAGS="-lm"
When compiling C++:
cmake -DCMAKE_CXX_FLAGS="-lm"
Source:stack overflow - How do I add a linker or compile flag in a CMake file?
Setting the compiler to use when calling cmake
CC=egcc CXX=eg++ cmake ../
Detecting Linux distribution
Add to CMakeLists.txt:
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
execute_process(
COMMAND cat /etc/os-release
OUTPUT_VARIABLE OS_RELEASE_OUTPUT
ERROR_VARIABLE OS_RELEASE_ERROR
RESULT_VARIABLE OS_RELEASE_RESULT
)
if(NOT OS_RELEASE_RESULT EQUAL 0)
message(WARNING "Failed to get OS release information: ${OS_RELEASE_ERROR}")
else()
if(OS_RELEASE_OUTPUT MATCHES "ID=alpine")
add_compile_definitions(ALPINE)
endif()
endif()
endif()
Source:LinuxVox.com - CMake Detection of Linux: A Comprehensive Guide
