我如何 link CMAKE 中的 GLFW 和亲爱的 ImGUI

How can i link GLFW and Dear ImGUI in CMAKE

最近几天我一直在搞 CMake,一切正常,除了我找不到 link ImGUI 和 GLFW 的方法

一切都是从源代码构建的

这是错误:

Consolidate compiler generated dependencies of target glad
[  2%] Built target glad
Consolidate compiler generated dependencies of target stb
[  5%] Built target stb
Consolidate compiler generated dependencies of target glfw
[ 29%] Built target glfw
Consolidate compiler generated dependencies of target imgui
[ 30%] Building CXX object CMakeFiles/imgui.dir/lib/imgui/imgui.cpp.o
[ 31%] Building CXX object CMakeFiles/imgui.dir/lib/imgui/imgui_demo.cpp.o
[ 33%] Building CXX object CMakeFiles/imgui.dir/lib/imgui/imgui_draw.cpp.o
[ 34%] Building CXX object CMakeFiles/imgui.dir/lib/imgui/imgui_tables.cpp.o
[ 36%] Building CXX object CMakeFiles/imgui.dir/lib/imgui/imgui_widgets.cpp.o
[ 37%] Building CXX object CMakeFiles/imgui.dir/lib/imgui/imgui_impl_glfw.cpp.o
/home/erik/Workspace/LearnOpenGL/lib/imgui/imgui_impl_glfw.cpp:45:10: fatal error: GLFW/glfw3.h: No such file or directory
   45 | #include <GLFW/glfw3.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/imgui.dir/build.make:146: CMakeFiles/imgui.dir/lib/imgui/imgui_impl_glfw.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:228: CMakeFiles/imgui.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

这是我的 CMakeLists.txt :

cmake_minimum_required(VERSION 3.1)

project(LearnOpenGL)


set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/source")
set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")

set(SOURCES "${SRC_DIR}/Main.cpp"
    )


add_executable(${PROJECT_NAME} ${SOURCES})

target_include_directories(${PROJECT_NAME} PRIVATE "${SRC_DIR}")

set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)


# GLFW

set(GLFW_DIR "${LIB_DIR}/glfw")

set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")

add_subdirectory("${GLFW_DIR}")

target_link_libraries(${PROJECT_NAME} "glfw" "${GLFW_LIBRARIES}")
target_include_directories(${PROJECT_NAME} PRIVATE "${GLFW_DIR}/include")
target_compile_definitions(${PROJECT_NAME} PRIVATE "GLFW_INCLUDE_NONE")


# Glad

set(GLAD_DIR "${LIB_DIR}/glad")

add_library("glad" "${GLAD_DIR}/src/glad.c")

target_include_directories("glad" PRIVATE "${GLAD_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "${GLAD_DIR}/include")
target_link_libraries(${PROJECT_NAME} "glad" "${CMAKE_DL_LIBS}")


# GLM 

set(GLM_DIR "${LIB_DIR}/glm")

add_subdirectory(${GLM_DIR})

target_link_libraries(${PROJECT_NAME} "glm")


# STB

set(STB_DIR "${LIB_DIR}/stb")

add_library("stb" "${STB_DIR}/src/stb_image.cpp")

target_include_directories("stb" PRIVATE "${STB_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "${STB_DIR}/include")
target_link_libraries(${PROJECT_NAME} "stb" "${CMAKE_DL_LIBS}")


# Freetype

set(FREETYPE_DIR "${LIB_DIR}/freetype")

add_subdirectory("${FREETYPE_DIR}")

target_link_libraries(${PROJECT_NAME} "${FREETYPE_LIBRARIES}")
target_include_directories(${PROJECT_NAME} PRIVATE "${FREETYPE_DIR}/include")


# ImGUI

set(IMGUI_DIR "${LIB_DIR}/imgui")

add_library("imgui" "${IMGUI_DIR}/imgui.cpp"
                  "${IMGUI_DIR}/imgui_demo.cpp"
                  "${IMGUI_DIR}/imgui_draw.cpp"
                  "${IMGUI_DIR}/imgui_tables.cpp"
                  "${IMGUI_DIR}/imgui_widgets.cpp"
                  
                  "${IMGUI_DIR}/imgui_impl_glfw.cpp"
                  "${IMGUI_DIR}/imgui_impl_opengl3.cpp")

target_include_directories("imgui" PRIVATE "${IMGUI_DIR}")
target_include_directories(${PROJECT_NAME} PRIVATE "${IMGUI_DIR}")
target_link_libraries(${PROJECT_NAME} "imgui" "${CMAKE_DL_LIBS}")

这是我的文件夹结构

 1. build
 2. lib
    -  freetype
    -  glad
    -  glfw
    -  glm
    -  imgui
    -  stb
 3. source
    -  Main.cpp
 4. CMakeLists.txt

这是我的问题:我如何告诉 imgui glfw 已经在这里并且他可以使用它?

提前致谢

您的构建应该只包含以下内容:

cmake_minimum_required(VERSION 3.20)
project(LearnOpenGL)

## Find dependencies
find_package(glfw3 REQUIRED)
find_package(glad REQUIRED)
find_package(glm REQUIRED)
find_package(freetype REQUIRED)
find_package(imgui REQUIRED)

# stb does not have a CMake build, but is header-only
find_path(STB_INCLUDE_DIRS "stb.h") 

## Create main executable
add_executable(main source/Main.cpp)
target_include_directories(main 
  PRIVATE 
    ${STB_INCLUDE_DIRS}
    "${CMAKE_CURRENT_LIST_DIR}/source"
)
target_link_libraries(
  main
  PRIVATE
    freetype
    glfw
    glad::glad
    glm::glm
    imgui::imgui
)

如果您不使用 CMake 3.20,(a) 您 应该 ,但 (b) 您可以尝试使用更早的版本。但是 永远不会 将版本放在您测试的版本之下。 CMake 只向后兼容,不向前兼容。

如果你只需要 freetype 作为 imgui 的依赖,那么你可以在 CMakeLists.txt.

中删除对它的引用

要构建它,您将想要 使用 vcpkg。为此,在 CMakeLists.txt 旁边创建一个名为 vcpkg.json 的文件,其中包含以下内容:

{
  "name": "learn-opengl",
  "version": "0.1.0",
  "dependencies": [
    "glfw3",
    "glad",
    "glm",
    "freetype",
    "stb",
    {
      "name": "imgui",
      "features": ["freetype", "glfw-binding", "opengl3-glad-binding"]
    }
  ]
}

这只是告诉 vcpkg 您需要哪些依赖项,以及您希望 imgui 构建有 freetype 支持和 glfw 绑定。

那么你只需要运行三个命令。

$ git clone https://github.com/microsoft/vcpkg.git
$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_TOOLCHAIN_FILE=$PWD/vcpkg/scripts/buildsystems/vcpkg.cmake
$ cmake --build build

这里唯一可能出乎意料的是设置CMAKE_TOOLCHAIN_FILE,这只是告诉CMake使用vcpkg。第一次 运行 时,vcpkg 将下载您的依赖项,构建它们,将它们缓存在 ~/.cache/vcpkg 中并将它们安装到您的构建文件夹中。随后的 运行 将构建 只是 您的代码。

这个序列在我的 Ubuntu 20.04 LTS 机器上成功地为我构建了你的代码。


为了使这更容易,考虑创建一个 CMakePresets.json 文件,也在您的根目录中,包含以下内容:

{
  "version": 2,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 20,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "default",
      "displayName": "Default Config",
      "description": "Default build using Make and vcpkg",
      "generator": "Unix Makefiles",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release",
        "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
      }
    },
    {
      "name": "default-msvc",
      "displayName": "Default MSVC",
      "description": "Default build using Visual Studio and vcpkg",
      "generator": "Visual Studio 16 2019",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
      }
    }
  ]
}

那么你可以 运行 cmake --preset=default 而不是上面更长的命令。