CMake if

cmake if

example

message(STATUS "env costmap_2d_INCLUDE_DIR " $ENV{costmap_2d_INCLUDE_DIR})
if (x"$ENV{costmap_2d_INCLUDE_DIR}" STREQUAL x"")
  find_package(costmap_2d REQUIRED)
  message(STATUS "costmap_2d_INCLUDE_DIRS " ${costmap_2d_INCLUDE_DIRS})
  set(costmap_2d_INCLUDE_DIR ${costmap_2d_INCLUDE_DIRS})
else()
  message(STATUS "get costmap_2d_INCLUDE_DIR from env")
  set(costmap_2d_INCLUDE_DIR $ENV{costmap_2d_INCLUDE_DIR})
endif()
message(STATUS "costmap_2d_INCLUDE_DIR " ${costmap_2d_INCLUDE_DIR})

STREQUAL example 2 !!

if("${CV_SIMD128}" STREQUAL "")
  add_definitions(-DCV_SIMD128=1)
else()
  add_definitions(-DCV_SIMD128=${CV_SIMD128})
endif()

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

cmake 2

cmake 2

use pkg-config in cmake

example

if($ENV{ROS_DISTRO} STREQUAL jade)
  message(STATUS "ROS_DISTRO is jade")
  find_package(PkgConfig REQUIRED)
  pkg_search_module(eigen3 REQUIRED eigen3)
  message(STATUS "eigen3_INCLUDE_DIRS " ${eigen3_INCLUDE_DIRS})
else()
  find_package(Eigen3 REQUIRED)
  message(STATUS "EIGEN3_INCLUDE_DIRS " ${EIGEN3_INCLUDE_DIRS})
endif()

see also and ref.
https://cmake.org/cmake/help/v3.0/module/FindPkgConfig.html

cmake link directories

example

message(STATUS "catkin_make LD_LIBRARY_PATH " $ENV{LD_LIBRARY_PATH})
link_directories($ENV{LD_LIBRARY_PATH})
set(LIBS
  # ros
  ${catkin_LIBRARIES}
  pcl_ros_tf
  # no ros
  ${PCL_LIBRARIES}
  ${octomap_LIBRARIES}
  boost_system)

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}")