使用cmake将两个解决方案合二为一

时间:2016-11-22 16:02:50

标签: c++ qt cmake vtk

我有两个独立的Visual Studio 2013解决方案,我想将它们迁移到一个解决方案中,因为第一个解决方案(使用Qt)充当第二个解决方案的GUI。

最后,我希望有一个结构如下的单一解决方案:

--Solution
-------All Build
-------Project 1
----------------External Dependencies
----------------header files
----------------Source files 
-------Project 2
----------------External Dependencies
----------------header files
----------------Source files 

第一个项目的CMakeLists.txt是:

cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
if(POLICY CMP0025)
  cmake_policy(SET CMP0025 NEW) # CMake 3.0
endif()
if(POLICY CMP0053)
  cmake_policy(SET CMP0053 NEW) # CMake 3.1
endif()

project (Project1)

set(CMAKE_MODULE_PATH 
    ${CMAKE_SOURCE_DIR}/CMake
)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

find_package(OpenVR REQUIRED)
find_package(SDL2 REQUIRED)

include_directories(${OPENVR_INCLUDE_DIRS})
include_directories(${SDL2_INCLUDE_DIR})
include_directories(${CMAKE_SOURCE_DIR})

set(HEADER_FILES
    ...
)

set(CODE_FILES
    ...
)


# Copy the obj file to the build directory
file(COPY Data/***.*** DESTINATION ${PROJECT_BINARY_DIR}/Data)
file(COPY Data/***.*** DESTINATION ${PROJECT_BINARY_DIR}/Data)
file(COPY Data/***.*** DESTINATION ${PROJECT_BINARY_DIR}/Data)

#QT5_ADD_RESOURCES(visualization_RCS resources.qrc)

if(VTK_LIBRARIES)
add_executable(${PROJECT_NAME} ${CODE_FILES} ${HEADER_FILES})
  target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES} ${SDL2_LIBRARY})  
else()
  message("Specific Libs")
endif()

第二个项目的CMakeLists.txt是:

cmake_minimum_required(VERSION 2.8.12)
project(Project2)
find_package(Qt5Widgets)
set(MyProjectLib_src ${PROJECT_SOURCE_DIR}/gui.cpp)
set(MyProjectLib_hdr ${PROJECT_SOURCE_DIR}/gui.h)
set(MyProjectLib_ui  ${PROJECT_SOURCE_DIR}/gui.ui)
set(MyProjectBin_src ${PROJECT_SOURCE_DIR}/main.cpp)
qt5_wrap_cpp(MyProjectLib_hdr_moc ${MyProjectLib_hdr})
qt5_wrap_ui (MyProjectLib_ui_moc  ${MyProjectLib_ui})
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_BINARY_DIR})
add_library(MyProjectLib STATIC 
    ${MyProjectLib_src}
    ${MyProjectLib_hdr_moc}
    ${MyProjectLib_ui_moc}
)
target_link_libraries(MyProjectLib Qt5::Widgets)
add_executable(MyProject ${MyProjectBin_src})
target_link_libraries(MyProject MyProjectLib)

问题是我无法将两个CMakeLists.txt合并到一个文件中。我从天以来就存在这个代码

cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
if(POLICY CMP0025)
  cmake_policy(SET CMP0025 NEW) # CMake 3.0
endif()
if(POLICY CMP0053)
  cmake_policy(SET CMP0053 NEW) # CMake 3.1
endif()

project (project1)

set(CMAKE_MODULE_PATH 
    ${CMAKE_SOURCE_DIR}/CMake
)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

find_package (Qt5Widgets REQUIRED)
find_package(OpenVR REQUIRED)
find_package(SDL2 REQUIRED)

include_directories(${OPENVR_INCLUDE_DIRS})
include_directories(${SDL2_INCLUDE_DIR})
include_directories(${CMAKE_SOURCE_DIR})

set(HEADER_FILES
    GPU/header.h
    ...
    ...
)

set(CODE_FILES
    GPU/sourcefile.cxx
    ...
)


#QT5_ADD_RESOURCES(visualization_RCS resources.qrc)
project (project2)


find_package (Qt5Widgets)


set (GuiLib_src Gui/gui.cpp)
set (GuiLib_hdr Gui/gui.h)
set (GuiLib_ui  Gui/gui.ui)
set (GuiBin_src Gui/main.cpp)


qt5_wrap_cpp(GuiLib_hdr_moc ${GuiLib_hdr})
qt5_wrap_ui (GuiLib_ui_moc  ${GuiLib_ui})


include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})


add_library (GuiLib STATIC 
    ${GuiLib_src}
    ${GuiLib_hdr_moc}
    ${GuiLib_ui_moc}
)
target_link_libraries (GuiLib Qt5::Widgets)



if(VTK_LIBRARIES)
  add_executable(${PROJECT_NAME} ${CODE_FILES} ${HEADER_FILES})
  target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES} ${SDL2_LIBRARY})  #Qt5::Core Qt5::Gui
  add_executable(Gui ${GuiBin_src})
  target_link_libraries (Gui GuiLib)
else()
  message("Specific Libs")
endif()

值得一提的是project1正在使用VTK,OpenVR和SDL,而project2正在使用Qt 5.

1 个答案:

答案 0 :(得分:2)

满足您在问题中列出的要求的最简单的解决方案是在Solution/CMakeLists.txt中引入新的CMakeList文件:

cmake_minimum_required(VERSION 2.8.12)
project(NameForTheCombinedProject)

add_subdirectory(path/to/project1/dir)
add_subdirectory(path/to/project2/dir)

此外,您必须将项目中对CMAKE_SOURCE_DIRCMAKE_BINARY_DIR的所有引用分别更改为CMAKE_CURRENT_SOURCE_DIRCMAKE_CURRENT_BINARY_DIR。特别是,这会影响项目1中的命令set(CMAKE_MODULE_PATH ...)file(COPY ...)

原因是现在CMAKE_SOURCE_DIRCMAKE_BINARY_DIR将引用顶层(新创建的)项目的目录。