使用cmake将两种解决方案合二为一 [英] Combining two solutions in one using cmake
问题描述
我有两个单独的Visual Studio 2013解决方案,由于第一个解决方案(使用Qt)用作第二个解决方案的GUI,因此我想将它们迁移到一个解决方案中。
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
第一个项目的 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
组合到一个文件中。自几天以来我就一直在使用此代码
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()
值得一提的是 project1
使用VTK,OpenVR和SDL,而 project2
仅使用Qt 5。
It is worth mentioning that project1
is using VTK, OpenVR and SDL, while project2
is just using Qt 5.
推荐答案
满足您在问题中概述的要求的最简单解决方案是在 Solution / CMakeLists中引入一个新的CMakeList文件。 txt
:
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)
此外,您还必须将所有引用更改为 CMAKE_SOURCE_DIR
和项目中的CMAKE_BINARY_DIR
分别为 CMAKE_CURRENT_SOURCE_DIR
和 CMAKE_CURRENT_BINARY_DIR
。特别是,这会影响Project中的命令 set(CMAKE_MODULE_PATH ...)
和 file(COPY ...)
1。
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.
原因是现在 CMAKE_SOURCE_DIR
和 CMAKE_BINARY_DIR
将引用顶级(新创建)项目的目录。
The reason is that now CMAKE_SOURCE_DIR
and CMAKE_BINARY_DIR
will refer to the directories of the toplevel (newly created) project.
这篇关于使用cmake将两种解决方案合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!