在Windows中使用CMake将可执行文件和DLL复制到用户指定的位置 [英] Copying executable and DLLs to User specified location using CMake in windows
问题描述
在CMake中,我想运行post build命令,该命令将可执行文件和必需的dll自动复制到用户指定的位置。
In CMake, I would like to run post build command which copies executable and required dll to User specified location automatically. Is it doable using CMake?
推荐答案
这取决于您要做什么。这是4种不同的解决方案。可能还有其他人要添加到此列表中...
It depends what do you want to do. Here is 4 different solutions. There is probably others to add to this list...
install()命令
如果您要复制刚刚构建的可执行文件和dll,则可以使用 install()
命令,但仅当用户运行 make install
时,此命令才有效。
If you want to copy executable and dll you just built you can use the install()
command but it will work only when the user run make install
.
设置变量
如果要在构建时直接进行操作,您可以使用CMake变量来配置构建。这些变量在 http://www.cmake.org/Wiki/CMake_Useful_Variables
If you want to do it directly at build time, you can use CMake variables to configure your build. These variables are described at http://www.cmake.org/Wiki/CMake_Useful_Variables
EXECUTABLE_OUTPUT_PATH
set this variable to specify a common place where CMake should put all executable files (instead of CMAKE_CURRENT_BINARY_DIR)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
LIBRARY_OUTPUT_PATH
set this variable to specify a common place where CMake should put all libraries (instead of CMAKE_CURRENT_BINARY_DIR)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
自定义命令
如果要复制其他未构建的可执行文件或dll(二进制库等),一个好的解决方案是使用自定义命令。在那种情况下,在所有操作系统上使用可移植的解决方案来复制文件可能非常棘手。这就是CMake直接从其 cmake
可执行文件提供此功能的原因:
If you want to copy other executable or dll you did not build yourself (binary libs, etc.), a good solution is to use a custom command. In that case it could be very tricky to have a portable solution working on all os to copy files. That's why CMake provide this feature (with others) directly from its cmake
executable:
在命令行中,您可以使用
In command line you could use that:
cmake -E copy_if_different <SOURCE> <DESTINATION>
别忘了您可以使用 $从CMakeLists文件调用cmake可执行文件{CMAKE_COMMAND}
变量;)
Don't forget you can call cmake executable from a CMakeLists file using ${CMAKE_COMMAND}
variable ;)
configure_file()命令
最后, configure_file()
命令允许您通过将变量替换为目标文件中变量的值来生成另一个文件。如果源文件不包含变量,则目标文件仅是源的副本。但是我不知道二进制文件是否可以正常使用。您应该自己仔细地进行测试。
And to finish, the configure_file()
command allows you to produce a file from another by replacing variables by their value in the target file. If the source file does not contains variables, the target file is only a copy of the source. But I don't know if it works perfectly with binary file. You should carefully test that by yourself.
这篇关于在Windows中使用CMake将可执行文件和DLL复制到用户指定的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!