0

所以我们有一组创建共享库、可执行文件和配置文件的项目,我希望它们最终都放在同一个目录中。我可以使用 EXECUTABLE_OUTPUT_PATH 和 LIBRARY_OUTPUT_PATH 将它们所有的二进制文件放到正确的位置(尽管能够只过滤掉这个目录的共享库会很好)。但是,配置文件也需要在这个目录中结束,我不知道如何做到这一点。

为了制作配置文件,我们使用:

foreach(config_file ${${PROJECT_NAME}_CONFIG_FILES})
  ADD_CUSTOM_COMMAND(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${config_file}"
    COMMAND cmake -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${config_file}"
    "${CMAKE_CURRENT_BINARY_DIR}/${config_file}"
    DEPENDS   "${CMAKE_CURRENT_SOURCE_DIR}/${config_file}"
    )
   list(APPEND ${PROJECT_NAME}_CONFIG_FILES_DEST "${CMAKE_CURRENT_BINARY_DIR}/${config_file}")
endforeach(config_file)

ADD_CUSTOM_TARGET(${PROJECT_NAME}_configuration_files ALL DEPENDS ${${PROJECT_NAME}_CONFIG_FILES_DEST})

创建自己的最终将配置文件放在正确的项目目录中,而不是二进制输出目录中。所以如果我设置 'EXECUTABLE_OUTPUT_PATH=LIBARY_OUTPUT_PATH=build\bin' 比我得到文件布局

+build
-+bin
----A.exe
----B.exe
----*.lib//don't need
----*.dll
-+A
----a.xml
-+B
----b.xml

这是没用的。我想要的是“PROJECT_OUTPUT_PATH”,所以无论是否设置了 EXECUTABLE_OUTPUT_PATH,我都可以将 xml 文件发送到正确的相应输出。有没有办法得到它?

4

1 回答 1

0

好吧,这行得通......它很丑陋,但它确实有效。

add_executable(${PROJECT_NAME}
    ${${PROJECT_NAME}_SRC_LIST}
    ${${PROJECT_NAME}_HDR_LIST}
    ${${PROJECT_NAME}_CONFIG_FILES})
#have to move after the executable add
get_property(${PROJECT_NAME}_location TARGET ${PROJECT_NAME} PROPERTY LOCATION)
GET_FILENAME_COMPONENT(${PROJECT_NAME}_location_path ${${PROJECT_NAME}_location} PATH)

# copy all configuration files to build directory
set(${PROJECT_NAME}_CONFIG_FILES service_config.xml)
if (NOT "${${PROJECT_NAME}_location_path}" MATCHES "${CMAKE_CURRENT_SOURCE_DIR}")
foreach(config_file ${${PROJECT_NAME}_CONFIG_FILES})
  ADD_CUSTOM_COMMAND(OUTPUT "${${PROJECT_NAME}_location_path}/${config_file}"
    COMMAND cmake -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${config_file}"
    "${${PROJECT_NAME}_location_path}/${config_file}"
    DEPENDS   "${CMAKE_CURRENT_SOURCE_DIR}/${config_file}"
    )
   list(APPEND ${PROJECT_NAME}_CONFIG_FILES_DEST "${${PROJECT_NAME}_location_path}/${config_file}")
endforeach(config_file)

ADD_CUSTOM_TARGET(${PROJECT_NAME}_configuration_files ALL DEPENDS ${${PROJECT_NAME}_CONFIG_FILES_DEST})
endif()

至少它适用于 Visual Studio 和 NMake。

于 2013-05-30T16:15:14.477 回答