设置生成目标的输出目录
1 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>) |
Note $<CONFIG>
应该在VS下才起作用
生成配置文件
比如生成版本信息,可以参考assimp
1 | PROJECT(Assimp VERSION 5.4.3) |
revision.h.in中的一部分如下:
1 |
PROJECT(Assimp VERSION 5.4.3)
会生成PROJECT_VERSION_MAJOR
、PROJECT_VERSION_MINOR
、PROJECT_VERSION_PATCH
变量。
后处理事件
拷贝目标所需文件到对应Configuration目录
参考自How to copy DLL files into the same folder as the executable using CMake?1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18# copy file
add_custom_command(TARGET MyTest POST_BUILD # Adds a post-build event to MyTest
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/libs/test.dll" # <--this is in-file
$<TARGET_FILE_DIR:MyTest>) # <--this is out-file path
# copy dir
add_custom_command(TARGET MyTest POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/libs"
$<TARGET_FILE_DIR:MyTest>)
# copy dir of specified configuration
add_custom_command(TARGET MyTest POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/libs/$<CONFIGURATION>"
$<TARGET_FILE_DIR:MyTest>)拷贝所有目标所需文件到对应Configuration目录
参考自Copy all files with given extension to output directory using CMake
方法一
1 | file(GLOB MY_PUBLIC_FILES "myDir/*.*" ) |
方法二
1 | install(DIRECTORY ${PROJECT_SOURCE_DIR}/myDir/ # 注意末尾的`/` |