find_package
-
参数
- REQUIRED: 如果指定了这个选项,CMake会在找不到包的时候失败。
- QUIET: 如果指定了这个选项,如果找不到包,CMake 将不会产生任何消息。
- MODULE: 如果指定了这个选项,CMake 将只搜索一个Find
.cmake文件。
-
假设文件结构:
1 2 3 |
CMakeLists.txt cmake/FindFoo.cmake cmake/FindBoo.cmake |
Module mode
- CMakeLists.txt
1 2 3 4 5 6 7 8 |
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") find_package(Foo REQUIRED) # FOO_INCLUDE_DIR, FOO_LIBRARIES find_package(Boo REQUIRED) # BOO_INCLUDE_DIR, BOO_LIBRARIES include_directories("${FOO_INCLUDE_DIR}") include_directories("${BOO_INCLUDE_DIR}") add_executable(Bar Bar.hpp Bar.cpp) target_link_libraries(Bar ${FOO_LIBRARIES} ${BOO_LIBRARIES}) |
Config mode (install)
- CMakeLists.txt
1 2 3 4 5 6 7 |
cmake_minimum_required(VERSION 2.8) project(Foo) add_library(foo Foo.hpp Foo.cpp) install(FILES Foo.hpp DESTINATION include) install(TARGETS foo DESTINATION lib) install(FILES FooConfig.cmake DESTINATION lib/cmake/Foo) |
- FooConfig.cmake
1 2 3 |
add_library(foo STATIC IMPORTED) find_library(FOO_LIBRARY_PATH foo HINTS "${CMAKE_CURRENT_LIST_DIR}/../../") set_target_properties(foo PROPERTIES IMPORTED_LOCATION "${FOO_LIBRARY_PATH}") |
Config mode (use)
- CMakeLists.txt
1 2 3 4 5 6 7 8 |
cmake_minimum_required(VERSION 2.8) project(Boo) # import library target `foo` find_package(Foo CONFIG REQUIRED) add_executable(boo Boo.cpp Boo.hpp) target_link_libraries(boo foo) |
通过宏来控制是否显示警告信息
1 2 3 |
function(set_no_deprecated_declarations_warning) add_definitions(-Wno-deprecated-declarations) endfunction() |
用option定义开关
1 2 3 4 5 6 7 8 9 10 11 12 |
function(cp_post_build) if(DEFINED CP_PORTINFO_LIB) if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND cp ${CMAKE_SOURCE_DIR}/cmake-build-debug/EnPortInfoColl.dylib ${CMAKE_SOURCE_DIR}/testPro/cmake-build-debug/EnPortInfoColl.dylib ) endif() endif() endfuntion() |
1 2 |
option(CP_PORTINFO_LIB BOOL ON) cp_post_build() |
判断平台
- 测试中发现,Darwin如果写成darwin,会导致message打印失效
1 2 3 4 5 6 7 8 9 |
if(CMAKE_SYSTEM_NAME STREQUAL "Linux") message("The system is Linux.") elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") message("The system is Windows.") elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") message("The system is macOS.") else() message("The system is unknown.") endif() |
判断CPU架构
1 2 3 4 5 6 7 8 9 |
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "amd64.*|x86_64.*|AMD64.*") target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/3rd/libcurl/lib_linux/libcurl_ssl_x86_64.a) target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/3rd/zlib-1.3/lib/libz_x86_64_64.a) elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64.*|arm64.*") target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/3rd/libcurl/lib_linux/libcurl_ssl_arm64.a) target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/3rd/zlib-1.3/lib_linux/libz_arm_64.a) else () message(FATAL_ERROR "not support...") endif() |
判断CPU位数
1 2 3 4 5 |
if(CMAKE_SIZEOF_VOID_P EQUAL 8) message("Target is 64 bits") elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) message("Target is 32 bits") endif() |
在IDE中更好地显示或组织源文件
- 通过在 source_group 命令中指定组名和对应的源文件列表,可以将源文件按组进行组织
- 在许多集成开发环境(IDE)中,例如 Visual Studio 和 Xcode,source_group 命令会在项目中创建对应的文件夹或组织结构,以便更好地管理和显示源文件
1 2 3 4 5 6 7 8 9 10 11 |
# 添加源文件 set(SOURCES src/main.cpp src/foo.cpp src/bar.cpp include/foo.h include/bar.h ) # 创建源文件组 source_group("Source Files" FILES ${SOURCES}) |
读写并修改文件内容
- 读文件
- 修改字符串
- 再写回文件
1 2 3 4 5 6 |
set(_helper_info_plist "${CMAKE_CURRENT_BINARY_DIR}/helper-Info${_target_suffix}.plist") file(READ "${CMAKE_CURRENT_SOURCE_DIR}/resources/mac/helper-Info.plist" _plist_contents) string(REPLACE "\${EXECUTABLE_NAME}" "${_helper_output_name}" _plist_contents ${_plist_contents}) string(REPLACE "\${PRODUCT_NAME}" "${_helper_output_name}" _plist_contents ${_plist_contents}) string(REPLACE "\${BUNDLE_ID_SUFFIX}" "${_plist_suffix}" _plist_contents ${_plist_contents}) file(WRITE ${_helper_info_plist} ${_plist_contents}) |
去掉dylib调试信息以及验证
- 我的环境是clion ide
- 使用的是默认的cc编译器,用下面命令可以看到,实际上cc指向的是clang
1 |
cc --version |
1 2 3 4 5 |
enlink@enlinkdeMBP cmake-build-debug % cc --version Apple clang version 14.0.0 (clang-1400.0.29.202) Target: arm64-apple-darwin22.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin |
- 我的clion里面生成工具也是用的默认的ninja,查看build.ninja发现,里面FLAGS带了-g
1 2 3 4 5 6 7 |
build CMakeFiles/EnSoftwareColl.dir/pch.cpp.o: CXX_COMPILER__EnSoftwareColl_Debug /Users/enlink/code/uecm/EnSysEnv/EnSoftwareColl/pch.cpp || cmake_object_order_depends_target_EnSoftwareColl DEFINES = -DDEBUG -DEnSoftwareColl_EXPORTS DEP_FILE = CMakeFiles/EnSoftwareColl.dir/pch.cpp.o.d FLAGS = -g -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -mmacosx-version-min=10.15 -fPIC -fcolor-diagnostics -Wno-deprecated-declarations -std=gnu++17 INCLUDES = -I/Users/enlink/code/uecm/EnSysEnv/EnSoftwareColl/../../3rd/boost_1_78_0_mac/include -I/Users/enlink/code/uecm/EnSysEnv/EnSoftwareColl/../../common -I/Users/enlink/code/uecm/EnSysEnv/EnSoftwareColl/../../common/mac_common OBJECT_DIR = CMakeFiles/EnSoftwareColl.dir OBJECT_FILE_DIR = CMakeFiles/EnSoftwareColl.dir |
- 无效的方法
1 2 |
set(CMAKE_C_FLAGS "") set(CMAKE_CXX_FLAGS "") |
- 有效的方法
1 |
set(CMAKE_CXX_FLAGS_DEBUG "") |
- 不直观的验证方法(我的环境)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 去掉之前 enlink@enlinkdeMBP cmake-build-debug % otool -l EnSoftwareColl.dylib| grep -A 4 "LC_SYMTAB" # 去掉之后res cmd LC_SYMTAB cmdsize 24 symoff 913656 nsyms 6385 stroff 1025624 # 去掉之后 enlink@enlinkdeMBP cmake-build-debug % otool -l EnSoftwareColl.dylib| grep -A 4 "LC_SYMTAB" # 去掉之后res cmd LC_SYMTAB cmdsize 24 symoff 913656 nsyms 27430 stroff 1362344 |
- 有效的验证方法(我的环境)
- 会在同级目录生成EnSoftwareColl.dylib.dSYM包
1 2 |
enlink@enlinkdeMBP cmake-build-debug % dsymutil EnSoftwareColl.dylib warning: no debug symbols in executable (-arch x86_64) |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ CMake教程二07/07
- ♥ Makefile记述一08/15
- ♥ CMake教程一06/20
- ♥ CLion:配置C++下Nasm开发环境(debian)08/06
- ♥ 使用Cmake构建工程:一05/17
- ♥ CMake生成器10/02