Combining two solutions in one using cmake

2019-07-07 17:17发布

问题:

I have two seperate Visual Studio 2013 solutions and I want to migrate them into one single solution since the first solution (using Qt) serves as the GUI for the second solution.

At the end I would like to have a single solution with a structure like this:

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

The CMakeLists.txt for the first project is:

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()

The CMakeLists.txt for the second project is:

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)

The thing is that i am not able to combine the two CMakeLists.txt into one file. I am stock at this code since days

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()

It is worth mentioning that project1 is using VTK, OpenVR and SDL, while project2 is just using Qt 5.

回答1:

The easiest solution to satisfy the requirements you have outlined in the question is to introduce a new CMakeList file in Solution/CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.12)
project(NameForTheCombinedProject)

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

In addition, you'll have to change all references to CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR in the projects to CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_BINARY_DIR, respectively. In particular, this affects the commands set(CMAKE_MODULE_PATH ...) and file(COPY ...) in Project 1.

The reason is that now CMAKE_SOURCE_DIRand CMAKE_BINARY_DIR will refer to the directories of the toplevel (newly created) project.



标签: c++ qt cmake vtk