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

时间:2015-02-04 23:15:21

标签: c++ cmake qt-creator

我创建了以下CMakeLists.txt,其中包含两个目标(源文件Client.cppServer.cpp,两者都取决于两个生成的文件,即Printer.hPrinter.cpp。文件由Printer.ice由ZeroC 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)

目录结构如下:

IceTest
|-- CMakeLists.txt
|-- client
    |-- Client.cpp
|-- ice
    |-- Printer.ice
    |-- Printer.cpp    <--- Generated
    |-- Printer.h      <--- Generated
|-- server
    |-- Server.cpp

在我的CMakeLists.txt中,我尝试将生成的文件Printer.hPrinter.cpp包含在IDE中(此处为QtCreator)。该文档指出,为方便起见,将添加SOURCES自定义目标(IceGenerated)添加到IDE中。但是,对于我的目标不是这种情况:生成的文件在IDE中不可见(它们是生成的并且已添加到可执行文件中)。我做错了什么?

0 个答案:

没有答案