1.find_package
##
find_package(PkgConfig)
pkg_check_modules(xxx)
include_diretories(${xxx_INCLUDE_DIRS})
find_package有两种搜索模式:
1.module
Find<package>.cmake
搜索CMAKE_MODULE_PATH路径下的Find<package>.cmake
2.Config
<packahe>OCnfig.cmake
3.none
对于可能没有***.cmake和***Config.cmake的库文件,可以直接找到其头文件和库文件所在文件夹,直接进行路径赋值:
SET(LAPACK_DIR /usr/local/lib/) SET(LAPACK_INCLUDE_DIRS /usr/local/include) SET(LAPACK_LIBRARIES /usr/local/lib)
##
install:
export
##
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/")
##
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
##
CMakeCache.txt的作用?
##
一般用set命令定义的变量能从父目录传递到子目录
##
add_subdirectory报错:
(When specifying an out-of-tree source a
binary directory must be explicitly specified.)
这是因为子模块不在父模块下,在其他路径,所以add_subdirectory需要写第二个参数,就是一个变量,用于保存submodule生成的路径
2.编译前环境检查和版本配置
##
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX14)
message(STATUS "yao enter CXX14")
set(CMAKE_CXX_STANDARD 14)
elseif(COMPILER_SUPPORTS_CXX0X)
message(STATUS "yao enter CXX0X")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "yao The compiler ${CMAKE_CXX_COMPILER} has no C++14 support. Please use a different C++ compiler.")
endif()
##
add_complie_options(-fPIC)
-fPIC:主要作用于动态库的生成,gcc默认设置
fPIC选项告诉编译器生成与位置无关的代码(即可以在运行时加载到任何特定虚拟内存地址的代码)。它的基本意思是编译器将引入一个额外的间接级别来访问静态/全局变量以及函数。
SET (CMAKE_C_COMPILER "/home/xxx/soft_ware/llvm/build/bin/clang") # 填写绝对路径
SET (CMAKE_C_FLAGS "-Wall -std=c99")
SET (CMAKE_C_FLAGS_DEBUG "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
SET (CMAKE_CXX_COMPILER "/home/xxx/soft_ware/llvm/build/bin/clang++") # 填写绝对路径
SET (CMAKE_CXX_FLAGS "-Wall")
SET (CMAKE_CXX_FLAGS_DEBUG "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")