结合Visual Studio 配置说明
工程名称
project(<name>)
常规
-
输出目录 (Win + VS环境下强行指定)
- Debug
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin)
- Release
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/bin)
- Linux 指定so文件输出
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
- Debug
-
目标文件名
- 后缀设置
set(CMAKE_DEBUG_POSTFIX "d")
- 重新命名
if(WIN32) if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(_output_suffix "_64") else() set(_output_suffix "") endif() else() set(_output_suffix "") endif() set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}${_output_suffix}")
-
平台工具集
if(MSVC OR MSVC_IDE) if( MSVC_VERSION EQUAL 1600 ) message("use Microsoft Visual Studio 2010") endif() endif()
-
类型配置
- exe
add_executable()
- lib/dll
name: 目标文件名add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [source1] [source2] [...])
STATIC:静态链接库
SHARED:动态链接库
MODULE:插件
- exe
-
字符集
add_definitions(-DUNICODE -D_UNICODE)
调试
- 环境
需要cmake版本支持 VS_DEBUGGER_ENVIRONMENT (3.13.2)set(RUN_ENVIROMENT xxx) set_target_properties(target PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=${RUN_ENVIROMENT}%3b$(PATH)")
C/C++
-
附加包含目录
include_directories(xxxxx)
-
预处理宏
add_dependencies(-Dxxxxx)
-
优化
if (MSVC) set(CompilerFlags CMAKE_CXX_FLAGS_RELWITHDEBINFO ) foreach(CompilerFlag ${CompilerFlags}) string(REPLACE "/O2" "/Od" ${CompilerFlag} "${${CompilerFlag}}") endforeach() endif(MSVC)
链接器
-
附加库目录
link_directories(xxxxx)
-
附加依赖项
target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> <item>... [<PRIVATE|PUBLIC|INTERFACE> <item>...]...) # 特定处理 target_link_libraries(<target> $<$<CONFIG:Debug>:xxxxxd> $<$<CONFIG:Release>:xxxxx> )
-
导入库
- Debug
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}/lib)
- Release
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}/lib)
- Linux 导出*.a路径
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
-
系统/子窗口
- set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS “/SUBSYSTEM:WINDOWS”)
其他设置说明
-
工程中的filters
- 头文件filters
set(${PROJECT_NAME}_SOURCES [source1][...])
- 自动查找指定后缀文件
#GLOB 指定目录 #GLOB_RECURSE 递归所有子目录 file(GLOB HEADERS *.h)
- 自动查找指定后缀文件
- 源文件filters
set(${PROJECT_NAME}_HEADERS [source1][...])
- 获得一个目录下的所有源文件
aux_source_directory(<dir> <variable>)
- 获得一个目录下的所有源文件
- 添加filters
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_SOURCES} ${${PROJECT_NAME}_HEADERS} )
- filters 分类 (需要重新加载工程)
source_group("Header Files\\xxxx" FILES ${HEADERS}) source_group("Source Files\\xxxx" FILES ${SOURCES})
- 头文件filters
-
自定义变量
set(OBJ_NAME xxxx),在非if语句中,使用${OBJ_NAME}引用,if中直接使用变量名引用 -
添加要编译的子目录
add_subdirectory(xxxxx) #合并 (Specify a unique binary directory name) add_subdirectory(XXX_dir "${CMAKE_BINARY_DIR}/XXX")
-
设置条件编译
option(DEBUG_mode "ON for debug or OFF for release" ON) IF(DEBUG_mode) add_definitions(-DDEBUG) ENDIF()
-
其他
可结合 *.vcxproj 查找需要的指令 -
*.cmake 文件使用
#设置CMAKE_MODULE_PATH 路径 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/") #添加文件名称 include(<name>)
-
对于RUNTIME 和 LIBRARY 两种目标,在安装时候,cmake会默认给你移除掉目标文件中的gcc的Wl,rpath的值,导致某些库找不到的错误。
set(CMAKE_INSTALL_RPATH ".") #需要在 add_subdirectory 之前添加 install (TARGETS <name> RUNTIME DESTINATION ${DESTINATION_PATH} LIBRARY DESTINATION ${DESTINATION_PATH} ARCHIVE DESTINATION ${DESTINATION_PATH} )
-
字符串比较
if("a" STREQUAL "a") endif()
-
可变参数获取
"${ARGV0}"
-
可变参数解析
function(parse_args)
set(options ADULT)
set(oneValueArgs NAME AGE)
set(multiValueArgs SCORE)
cmake_parse_arguments(_arg
"${options}" "${oneValueArgs}" "${multiValueArgs}"
${ARGN} )
message("isadult = ${_arg_ADULT}")
message("name = ${_arg_NAME}")
message("age = ${_arg_AGE}")
message("score = ${_arg_SCORE}")
endfunction()
parse_args(ADULT NAME zhangsan AGE 20 SCORE 100 200 300 400 500)
- 创建文件
file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
"#ifdef __cplusplus\n"
"# error \"The CMAKE_C_COMPILER is set to a C++ compiler\"\n"
"#endif\n"
"#if defined(__CLASSIC_C__)\n"
"int main(argc, argv)\n"
" int argc;\n"
" char* argv[];\n"
"#else\n"
"int main(int argc, char* argv[])\n"
"#endif\n"
"{ (void)argv; return argc-1;}\n")
参考文档
https://blog.csdn.net/zhangzq86/article/details/80718559