cmake

cmake

see also https://yuiwong.org/gitlab/documentation/compiler

cmake-compileflags

  • CFLAGS
  • CXXFLAGS

#


howto

  • via add_definitions

add_definitions

Adds -D define flags to the compilation of source files.

add_definitions(-DFOO -DBAR ...)

Adds definitions to the compiler command line for targets in the current
directory and below
(whether added before or after this command is invoked).
This command can be used to add any flags,
but it is intended to add preprocessor definitions
(see the add_compile_options() command to add other flags).
Flags beginning in -D or /D that look like preprocessor definitions are
automatically added to the COMPILE_DEFINITIONS directory property
for the current directory.
Definitions with non-trivial values may be left in the set of flags
instead of being converted for reasons of backwards compatibility.
See documentation of the directory, target,
source file COMPILE_DEFINITIONS properties for details
on adding preprocessor definitions to specific scopes and configurations.

See the cmake-buildsystem(7) manual for more on defining
buildsystem properties

  • via set, see below example

  • via SET_TARGET_PROPERTIES

example

SET_TARGET_PROPERTIES(demo PROPERTIES COMPILE_FLAGS "\
  -std=c++11 \
  -Wall -Wextra")

cpp std

example

add_definitions(-std=c++11)

example 2

# check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  add_definitions(-DCOMPILEDWITHC11)
  message(STATUS "using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
  add_definitions(-DCOMPILEDWITHC0X)
  message(STATUS "using flag -std=c++0x.")
else()
  message(FATAL_ERROR
    "the compiler ${CMAKE_CXX_COMPILER} has no C++11 support. "
   "please use a different C++ compiler.")
endif()

warning

example

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wundef")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunreachable-code")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Winline")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")

reference

external

usually slow ..

how to define

example

cmake .. -DCMAKE_INSTALL_PREFIX=/opt/aaa

prefix

CMAKE_INSTALL_PREFIX

install

  • TARGETS

example

install(TARGETS ${PROJECT_NAME}
  LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)

example 2

install(TARGETS
  linetest
  RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)

master-sub-cmakes

  • master CMakeLists.txt “make into” sub-directories via add_subdirectory:
add_subdirectory(SOME-PATH)
  • use add_custom_target, example
add_custom_target(config_Pangolin
  mkdir -p ${CMAKE_BINARY_DIR}/libs/Pangolin
  && cd ${CMAKE_BINARY_DIR}/libs/Pangolin
  && ${CMAKE_COMMAND} ${PROJECT_SOURCE_DIR}/libs/Pangolin
  -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/install/Pangolin
  -DCMAKE_CXX_FLAGS="-I${librealsense_INCLUDE_DIRS}")