作为一个例子,假设有四个文件夹(app1,app2,app3和main),如下面的
main
|__ CMakeLists.txt
\__ module1
|______ CMakeLists.txt
|______ sub1.cpp
|______ sub1.h
\__ library5
|______ CMakeLists.txt
|______ sub5.cpp
|______ sub5.h
\__app1
\__app2
\__app3
module1的哪个输出是module1.dll,library5的输出是lib5.dll。 app1的文件夹必须包含module1.dll和lib5.dll,app2需要lib5.dll,最后app3需要module1.dll(应用程序,模块和库的数量超过这个例子,我在下面解释我们不想要要更改模块/库CMakeLists.txt
,只需要主要CMakeLists.txt
是我们的。{/ p>
PS:
我有一个cmake
项目,它有几个库和模块。它们使用add_subdirectory
命令包含在我的项目中(请注意,我的项目仅由多个模块组成,并且没有add_library
或add_target
。
我需要复制库/模块 的输出而不更改CMakeLists.txt
{add_custom_command
POST_BUILD
选项实际不是是一个不错的选择,因为此时我需要更改它们不属于我的项目的库/模块的CMakeLists.txt
。另一方面,必须在外部(主要) CMakeLists.txt
完成,其中包含其他人(图书馆/模块)。
我尝试了其他一些命令,例如file (COPY )
和configure_file()
,但我认为它们可以生成 cmake-cache
阶段,只需复制存在的资源文件在预建阶段。
此外,在另一种方法中,我编写了一个bash脚本文件来复制文件,并通过bellow命令在主CMakeLists.txt
中调用它。
add_custom_target (copy_all
COMMAND ${CMAKE_SOURCE_DIR}/copy.sh ${files}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
files
包含文件列表。但副本没有执行!我手动测试脚本,它根据需要工作。但我不知道为什么它无法在CMakeLists.txt中调用。
如何将子项目输出复制到主要CMakeLists.txt
的某些位置?
答案 0 :(得分:1)
为了简化一点,让我们说你有:
<强>的CMakeLists.txt 强>
cmake_minimum_required(VERSION 3.0)
project(PostBuildCopyFromRoot)
add_subdirectory(module)
<强>模块/的CMakeLists.txt 强>
file(WRITE "module.h" "int ModuleFunc();")
file(WRITE "module.cpp" "int ModuleFunc() { return 1; }")
add_library(module SHARED "module.cpp" "module.h")
target_include_directories(module PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
set_target_properties(module PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1)
应用/ app.mexw64 强>
如果您现在只需将以下内容添加到根CMakeLists.txt
:
add_custom_command(
TARGET module
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"$<TARGET_FILE:module>"
"app/$<TARGET_FILE_NAME:module>"
)
你将从CMake获得:
CMake Warning (dev) at CMakeLists.txt:8 (add_custom_command): Policy CMP0040 is not set: The target in the TARGET signature of add_custom_command() must exist. Run "cmake --help-policy CMP0040" for policy details. Use the cmake_policy command to set the policy and suppress this warning. TARGET 'module' was not created in this directory.
您始终可以覆盖命令行为:
function(add_library _target)
_add_library(${_target} ${ARGN})
add_custom_command(
TARGET ${_target}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"$<TARGET_FILE:${_target}>"
"${CMAKE_SOURCE_DIR}/app/$<TARGET_FILE_NAME:${_target}>"
)
endfunction()
注意:在<{em> add_subdirectory()
电话