gpt4 book ai didi

c++ - 使用cmake将两种解决方案合二为一

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:50 24 4
gpt4 key购买 nike

我有两个独立的 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。

最佳答案

满足您在问题中列出的要求的最简单解决方案是在 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 将引用顶层(新创建的)项目的目录。

关于c++ - 使用cmake将两种解决方案合二为一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40746444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com