如何在 CMake 中包含生成的源文件以在 IDE 中显示它们?

won*_*ing 5 c++ cmake qt-creator

我创建了以下 CMakeLists.txt,它具有两个目标(源文件Client.cppServer.cpp,都取决于两个生成的文件,即Printer.hPrinter.cpp。这些文件是由Printer.iceZeroC Ice 可执行文件生成的slice2cpp

cmake_minimum_required(VERSION 3.1.1)
project(IceTest)

set(CMAKE_VERBOSE_MAKEFILE true)

file(GLOB ice_SLICE
    "${PROJECT_SOURCE_DIR}/ice/*.ice"
)

string(REPLACE ".ice" ".h" ice_HDR ${ice_SLICE})
string(REPLACE ".ice" ".cpp" ice_SRC ${ice_SLICE})

file(GLOB server_HDR
    "server/*.h"
)

file(GLOB server_SRC
    "server/*.cpp"
)

file(GLOB client_HDR
    "client/*.h"
)

file(GLOB client_SRC
    "client/*.cpp"
)

find_package(Ice REQUIRED Ice IceUtil)

add_custom_command(
    OUTPUT ${ice_HDR} ${ice_SRC}
    COMMAND ${Ice_SLICE2CPP_EXECUTABLE} ${ice_SLICE}
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/ice
)

add_custom_target(
    Slice2Cpp
    DEPENDS ${ice_HDR} ${ice_SRC}
    COMMENT "Running Slice2Cpp..."
    SOURCES ${ice_SLICE}
)

add_custom_target(
    IceGenerated
    DEPENDS Slice2Cpp
    SOURCES ${ice_HDR} ${ice_SRC}
)

include_directories(IceTestServer ${PROJECT_SOURCE_DIR}/ice ${PROJECT_SOURCE_DIR}/server ${Ice_INCLUDE_DIRS})
include_directories(IceTestClient ${PROJECT_SOURCE_DIR}/ice ${PROJECT_SOURCE_DIR}/client ${Ice_INCLUDE_DIRS})

add_executable(IceTestServer ${ice_HDR} ${ice_SRC} ${server_HDR} ${server_SRC})
add_executable(IceTestClient ${ice_HDR} ${ice_SRC} ${client_HDR} ${client_SRC})

add_dependencies(IceTestServer IceGenerated)
add_dependencies(IceTestClient IceGenerated)

target_link_libraries(IceTestServer ${Ice_LIBRARIES} pthread)
target_link_libraries(IceTestClient ${Ice_LIBRARIES} pthread)
Run Code Online (Sandbox Code Playgroud)

目录结构如下:

IceTest
|-- CMakeLists.txt
|-- client
    |-- Client.cpp
|-- ice
    |-- Printer.ice
    |-- Printer.cpp    <--- Generated
    |-- Printer.h      <--- Generated
|-- server
    |-- Server.cpp
Run Code Online (Sandbox Code Playgroud)

在我的文章中,CMakeLists.txt我尝试将生成的文件包含Printer.hPrinter.cppIDE 中(此处为 QtCreator)。文档指出,为了方便起见,将SOURCES自定义目标 ( IceGenerated) 添加到 IDE 中。然而,我的目标并非如此:生成的文件在 IDE 中不可见(尽管它们是生成并添加到可执行文件中的)。我究竟做错了什么?