无法使用CMake将.dll文件复制到可执行文件夹中

时间:2017-04-15 01:20:08

标签: dll cmake

通过这个答案:

How to copy DLL files into the same folder as the executable using CMake?

我正在尝试将SDL2.dll复制到我的.exe存储位置。但是,这对我不起作用。这是我的带有add_custom_command的CMakeLists.txt文件,理论上,它负责将dll复制到目标exe目录:

cmake_minimum_required(VERSION 2.8.11)

project(Another)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

add_executable(Test "${CMAKE_SOURCE_DIR}/SourCe/MAin.cpp")

find_path(SDL2_INCLUDE_DIR SDL.h HINTS "${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/Include/")
find_library(SDL2_LIBRARY_DIR SDL2 HINTS "${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/Lib/x86")
find_library(SDL2MAIN_LIBRARY_DIR SDL2main HINTS "${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/Lib/x86")

add_custom_command(TARGET Test POST_BUILD
     COMMAND ${CMAKE_COMMAND} -E copy_directory
         "${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86/SDL2.dll"
         $<TARGET_FILE_DIR:Test>)

include_directories(${SDL2_INCLUDE_DIR})
target_link_libraries(Test ${SDL2_LIBRARY_DIR})
target_link_libraries(Test ${SDL2MAIN_LIBRARY_DIR})

这是我项目中唯一的CMakelists.txt文件,所以我没有改变我的exe存储位置(默认情况下它似乎位于'debug'下的build文件夹中)。我还是cmake的新手,所以我想知道是否有人可以引导我朝着错误的方向前进。谢谢!

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。我正在寻找的命令是“copy_if_different”,而不是add_custom_command()函数中的“copy_directory”。