常用CMake命令总结
message():打印信息
message(STATUS " version: ${OpenCV_VERSION}")
- add_executable():编译出一个可执行文件
- add_executable(ctest test.cc) // 生成ctest可执行文件
- add_library():编译出一个库文件(需指定生成静态(STATIC)或动态(SHARED)/动态库)
- add_library(CheckLicenseJava SHARED check_license_jni.cc) // 生成libcheck_license_java.so动态库
- set()/target_link_libraries()/target_include_directories()
set(): 设置变量(可以是具体的库文件.so/.h, 也可以是一个文件夹)
- set(PICASSOLIB ${CURR_ROOT_DIR}/lib/libcheck_license.so)
- set(PICASSOINC ${CURR_ROOT_DIR}/include/)* set()连同add_executable()和add_library(),可以将.so/.h文件添加到可执行文件或者新的动态链接库中
target_link_libraries(): 将设置的库变量添加到可执行文件或者新的动态链接库中
target_include_directories():将设置的头文件变量添加到可执行文件或者新的动态链接库中
target_link_libraries(<target><PRIVATE|PUBLIC|INTERFACE> <item>...[<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
- target_link_libraries(ctest ${PICASSOLIB})
- target_include_directories(ctest PUBLIC ${PICASSOINC})
- find_package():查找工具依赖包
find_package(<PackageName> [version] [EXACT] [QUIET] [MODULE][REQUIRED] [[COMPONENTS] [components...]][OPTIONAL_COMPONENTS components...][NO_POLICY_SCOPE])- <PackageName>:待查找的包名
- [version]:待查找的包的版本
- [EXACT]:必须是精确的版本匹配
- [REQUIRED]:若找不到包,则终止cmake过程* 使用find_package()实际上查找的是<package_name>Config.cmake,当找到目标后会自动获取3个变量的值:<PackageName>_DIR、<PackageName>_INCLUDE_DIRS和<PackageName>_LIBS。find_package(OpenCV 3.4.16 REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS}) // 可选,将OpenCV的头文件路径添加到编译器的搜索路径中
编译一个简单的项目
当需要编译一个简单的项目,生成动态链接库以及可执行文件,可以进行参考。
一次编译生成动态链接以及可执行文件时,一般来说可执行文件加载的仍是源代码。所以如果还要验证动态链接库的可用性,最好再新建一个项目和CMakeLists.txt,通过
target_link_libraries()
链接动态链接库生成一个新的可执行文件来验证。
易犯错误:以下方文件tree为例,core文件夹中包含了一些需要链接的文件,在core对应的CMakeLists.txt中需要将所有文件链接为libcore.a/.so。而在src中的test.cc中需要使用core中这些接口,所以在src的CMakeLists.txt中必须要链接core,否则虽然能够cmake ..
通过,但是make -j
时会提示test.cc所使用的core中的很多方法未定义。
文件树
|-- CMakeLists.txt
|-- build
| |-- CMakeCache.txt
| |-- CMakeFiles
| |-- Makefile
| |-- bin
| |-- cmake_install.cmake
| |-- core
| |-- example
| |-- lib
| -- src
|-- cmake
| -- aarch64-linux-gnu.toolchain.cmake
|-- config.ini
|-- core
| |-- CMakeLists.txt
| |-- common
| |-- define.h
| |-- encrypt
| |-- flatbuffers
| |-- io.cc
| |-- io.h
| -- json
|-- example
| |-- CMakeLists.txt
| -- test.cc
|-- include
| |-- check_license.h
| -- slog.hpp
|-- src
| |-- CMakeLists.txt
| -- check_license.cc
-- aa.txt
core中对应的CMakeLists.txt,将所有文件打包为core。
# 将所有文件存储在src中; CMAKE_CURRENT_SOURCE_DIR:./core/
file(GLOB src ${CMAKE_CURRENT_SOURCE_DIR}/*.cc${CMAKE_CURRENT_SOURCE_DIR}/*.c${CMAKE_CURRENT_SOURCE_DIR}/common/*.*${CMAKE_CURRENT_SOURCE_DIR}/json/*.*) # 新增set(LIB_TYPE STATIC)
# 导入./check_license/core/encrypt/libencrypt_x86_64.a库
add_library(encrypt ${LIB_TYPE} IMPORTED GLOBAL)
set_target_properties(encrypt PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/encrypt/libencrypt_x86_64.a_ori) # libencrypt_${arch}.${LIB_TAIL}# 添加编译选项
if(NOT SHARED_LIB)add_compile_options(-fPIC -std=c++11)
endif()
# 创建core库
add_library(core ${LIB_TYPE} ${src})target_link_libraries(core encrypt)set_target_properties(core PROPERTIES FOLDER "xxxxxxx")
Message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})add_custom_target(core_installCOMMAND ${CMAKE_COMMAND} -E make_directory coreCOMMAND ${CMAKE_COMMAND} -E make_directory core/cmakeCOMMAND ${CMAKE_COMMAND} -E copy lib/libcore.${LIB_TAIL} core/COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/flatbuffers core/flatbuffersCOMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/common/licence.h core/commonCOMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/common/io.h core/commonCOMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/common/common.h core/commonCOMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/common/md5.h core/commonCOMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/common/sys.h core/commonCOMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/common/MachineInfo_generated.h core/commonCOMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/cmake/modules/FindGFlags.cmake core/cmake# COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/cmake/modules/FindGlog.cmake core/cmakeCOMMENT "make core install"WORKING_DIRECTORY ${PROJECT_BINARY_DIR})set_target_properties(core_install PROPERTIES FOLDER "xxxxxx")
src中的CMakeLists.txt。
# 将 src 中的文件编译为库(SHARED:动态库; STATIC:静态库)
add_library(check_license SHAREDcheck_license.cc
)# 添加 include 目录(确保头文件可以被找到)
target_include_directories(check_license PUBLIC ${CMAKE_SOURCE_DIR}/include)
# 链接core,否则无法找到core中方法,无法编译通过
target_link_libraries(check_license PUBLIC core)