【发布时间】:2019-08-29 04:31:33
【问题描述】:
我是 cmake 的新手,我尝试创建一个需要一些 3rd-party 库的小项目。我希望将库作为 git repos 始终保持最新。一些库只是 .cpp 和 .hpp 文件(glad、imgui),而其他库是 cmake 项目(glfw、glm)。
这个想法是有一个包含所有库的第 3 方项目作为一种子项目和一个使用库和包含等的沙盒项目。 而且我想使用不会在框架结构之外安装任何东西的现代 cmake 代码。
文件夹结构:
Framework
|--3rd_party
| |--glad
| | |--include
| | |--src
| |--glfw-master
| | |--...
| | |--CMakeLists.txt
| |--glm-master
| | |--..
| | |--CMakeLists.txt
| |--imgui-master
| | |--*.cpp
| | |--*.hpp
| | |--examples
| | | |--*.cpp
| | | |--*.hpp
| |--CMakeLists.txt
|--sandbox
| |--main.cpp
| |--CMakeLists.txt
|--CMakeLists.txt
所以我创建了这个文件夹结构以及一些 CMakeLists:
CMakeLists.txt(框架)
cmake_minimum_required(VERSION 3.10)
project(Framework)
add_subdirectory("3rd_party")
add_subdirectory("sandbox")
CMakeLists.txt (3rd_party)
#GLFW
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(glfw-master)
# GLM
set(GLM_TEST_ENABLE OFF CACHE BOOL "" FORCE)
add_subdirectory(glm-master)
# Glad
add_library(
Glad STATIC
"glad/src/glad.c"
)
target_include_directories(Glad PUBLIC "glad/include")
# ImGui
add_compile_definitions(IMGUI_IMPL_OPENGL_LOADER_GLAD=1)
set(IMGUI_SOURCES
"imgui-master/imgui.cpp"
"imgui-master/imgui_demo.cpp"
"imgui-master/imgui_draw.cpp"
"imgui-master/imgui_widgets.cpp"
"imgui-master/examples/imgui_impl_glfw.cpp"
"imgui-master/examples/imgui_impl_opengl3.cpp"
)
set(IMGUI_HEADERS
"imgui-master/imconfig.h"
"imgui-master/imgui.h"
"imgui-master/imgui_internal.h"
"imgui-master/imstb_rectpack.h"
"imgui-master/imstb_textedit.h"
"imgui-master/imstb_truetype.h"
"imgui-master/examples/imgui_impl_glfw.h"
"imgui-master/examples/imgui_impl_opengl3.h"
)
add_library(
ImGui STATIC
${IMGUI_SOURCES}
${IMGUI_HEADERS}
)
target_include_directories(ImGui PUBLIC "imgui-master" "glfw-master/include" "glad/include")
CMakeLists.txt(沙盒)
project(Sandbox)
find_package(OpenGL REQUIRED)
add_executable(sandbox main.cpp)
# OpenGL
target_include_directories(Sandbox PUBLIC ${OPENGL_INCLUDE_DIR})
target_include_directories(Sandbox PUBLIC external)
# Glfw
target_include_directories(Sandbox PUBLIC "../3rd_party/glfw-master/include")
# Link libs
target_link_libraries(Sandbox PUBLIC
${OPENGL_LIBRARIES}
"../3rd_party/glfw-master/src/Debug/glfw3"
Glad
ImGui
glm_static
)
代码有效,但与我预期的不同。起初我知道这有点难看,也许有更好的方法来处理包含和源的路径,但更大的问题是项目结构。 例如,当我为 ms vs studio 构建它时,我有三个解决方案
./framework.sln
./sandbox/sandbox.sln
./3rd_party/glfw-master/glfw.sln
很高兴,glm 和 imgui 是 sandbox.sln 的一部分
我想要的是一个包含两个子解决方案沙箱和 3rd_party 的解决方案,其中还包含所有库的子解决方案或项目。
那么有可能吗?如果可以,我如何使用 cmake 创建这样的结构?
【问题讨论】:
标签: cmake