cmake学习笔记
1.cmake相对路径
在android平台可以采用相对路径的方式
在windows平台相对路径不能生效,需要通过绝对路径获取。我采用的方法是获取当前目录工程根目录,然后获取上级目录的方法(比较笨),网友还提供了通过STRING方法,然后通过正则获取想要的目录
- 获取CMAKE_SOURCE_DIR上级目录
get_filename_component(PARENT_DIR $\{CMAKE_SOURCE_DIR\} DIRECTORY)
message(STATUS ${PARENT_DIR})
2.cmake设置ios安装路径
set_target_properties(test PROPERTIES BUILD_WITH_INSTALL_RPATH 1 INSTALL_NAME_DIR "@rpath")
3.cmake打印信息
message(STATUS "CMAKE_SOURCE_DIR:${CMAKE_SOURCE_DIR}!")-->打印正常信息
4.cmake各种目录设置
- 方法1:
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Bin)
- 方法2:
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/Bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/Lib)
- 方法3:(分别设置debug和release目录)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/Lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/Lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/Bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/Lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/Lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/Bin)
5.VS工程设置目录结构
- cmake中用于设置目录的方法为:
source_group(<name> [FILES <src>...] [REGULAR_EXPRESSION <regex>])
source_group(TREE <root> [PREFIX <prefix>] [FILES <src>...])
可以使用宏定义,保持工程结构和工程目录相同
macro(source_group_by_dir source_files)
if(MSVC)
set(sgbd_cur_dir ${CMAKE_CURRENT_SOURCE_DIR})
foreach(sgbd_file ${${source_files}})
string(REGEX REPLACE ${sgbd_cur_dir}/\(.*\) \\1 sgbd_fpath ${sgbd_file})
string(REGEX REPLACE "\(.*\)/.*" \\1 sgbd_group_name ${sgbd_fpath})
string(COMPARE EQUAL ${sgbd_fpath} ${sgbd_group_name} sgbd_nogroup)
string(REPLACE "/" "\\" sgbd_group_name ${sgbd_group_name})
if(sgbd_nogroup)
set(sgbd_group_name "\\")
endif(sgbd_nogroup)
source_group(${sgbd_group_name} FILES ${sgbd_file})
endforeach(sgbd_file)
endif(MSVC)
endmacro(source_group_by_dir)
- 使用方法:
AUX_SOURCE_DIRECTORY(${CMAKE_SOURCE_DIR}/src SRCS_LIST)
source_group_by_dir(SRCS_LIST)
ADD_LIBRARY (Test STATIC ${SRCS_LIST})
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 银河驿站
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果