我有一个包含三个二进制文件的项目,我正在使用CMake构建。一个是可执行文件,两个是DLL。 CMake只有一个DLL被自动复制到可执行文件的构建目录中;另一个不是。自动复制的DLL是C ++ / CLI DLL,而不是本机DLL,但链接到expat,它是我项目外部的DLL。
下面是四个CMakeLists.txt文件(名称已更改)。
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(my_project VERSION 0.1)
find_package(EXPAT REQUIRED)
add_library(expat INTERFACE IMPORTED)
set_target_properties(
expat
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${EXPAT_INCLUDE_DIRS}"
)
add_subdirectory(my_exe)
add_subdirectory(my_managed_dll)
add_subdirectory(my_dll_using_expat)
my_exe /的CMakeLists.txt:
set(
SOURCES
...
)
add_executable(my_exe ${SOURCES})
target_compile_definitions(my_exe PRIVATE _UNICODE)
target_include_directories(
my_exe
PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include"
PRIVATE include
)
target_link_libraries(my_exe my_managed_dll my_dll_using_expat)
configure_file(include/version.in.h include/version.h ESCAPE_QUOTES @ONLY)
add_custom_command(
TARGET my_exe POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"$<TARGET_FILE_DIR:my_managed_dll>/my_managed_dll.dll.config"
"$<TARGET_FILE_DIR:my_exe>/my_exe.exe.config"
)
# Had to add this to also copy expat DLL.
add_custom_command(
TARGET my_exe POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy_if_different
"$<TARGET_FILE_DIR:my_dll_using_expat>/expat.dll"
"$<TARGET_FILE_DIR:my_exe>"
)
my_managed_dll /的CMakeLists.txt:
set(
SOURCES
...
)
add_library(my_managed_dll SHARED ${SOURCES})
target_compile_options(my_managed_dll PRIVATE /clr /EHa)
# We need to remove this flag for CLR support to work.
string(REPLACE /RTC1 "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
target_compile_definitions(my_managed_dll PRIVATE _UNICODE)
target_include_directories(my_managed_dll PUBLIC include PRIVATE src)
add_custom_command(
TARGET my_managed_dll POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/src/my_managed_dll.config"
"$<TARGET_FILE_DIR:my_exe>/my_exe.dll.config"
)
my_dll_using_expat /的CMakeLists.txt:
set(
SOURCES
...
)
add_library(my_dll_using_expat SHARED ${SOURCES})
target_compile_definitions(my_dll_using_expat PRIVATE _UNICODE)
target_include_directories(
my_dll_using_expat
PUBLIC include
PUBLIC ${EXPAT_INCLUDE_DIRS}
PRIVATE src
)
target_link_libraries(my_dll_using_expat ${EXPAT_LIBRARIES})
add_custom_command(
TARGET my_dll_using_expat POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy_if_different
"${EXPAT_INCLUDE_DIR}/../bin/expat.dll"
"$<TARGET_FILE_DIR:my_dll_using_expat>"
)