CMake 学习入门

CMake 学习入门

配置

  • CMakeList.txt 配置
    • 配置cmake版本要求
    • 配置工程
    • 添加可执行文件
    • note: cmake文件不区分大小写
cmake_minimum_required(VERSION 2.6)
project(Tutorial)
add_executable(Tutorial tutorial.cxx)
  • 版本号设置,使用set()函数
# The version number
set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
  • 配置头文件
# configure a header file to pass some of the CMake settings to the source code
configure_file(
	"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
	"${PROJECT_BINARY_DIR}/TuturialConfig.h"
)
# add the binary tree to the search path for include files so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
  • 增加.in文件,联系工程文件与cmakelist的文件
//the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

note: 当CMake配置这个头文件时,”@Tutorial_VERSION_MAJOR@”和”@Tutorial_VERSION_MINOR@”的值将会被CMakeLists.txt文件中的值替换

添加一个库

  • 通过add_library()函数实现

    • MathFunctions的文件下,存在mysqrt.cxx
    add_library(MathFunctions mysqrt.cxx)
    
  • 在顶层目录的cmakelist.txt中添加链接目录

include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) 
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
  • 添加依赖可选

# should we use our own math functions?
option (USE_MYMATH  
        "Use tutorial provided math implementation" ON) 
  • 调整链接目录
# add the MathFunctions library?
#
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial  ${EXTRA_LIBS})
  • 为了在工程中使用USE_MYMATH,需要在.in文件中添加如下指令
#cmakedefine USE_MYMATH

你可能感兴趣的:(PX4,Cmake,makefile)