如何在 Windows 11 上将 ImGui、GLFW 和 OpenGL 与 vcpkg、Clion 和 CMake 一起使用

问题描述 投票:0回答:1

我使用vcpkg清单模式来安装软件包,

vcpkg.json
:

{
  "name": "vcpkgtest",
  "version": "1.0.0",
  "description": "learn vkpg & glfw & imgui & cmake",
  "dependencies": [
    "glfw3",
    {
      "name": "imgui",
      "features": ["glfw-binding", "opengl3-binding"]
    }
  ]
}

并安装成功,

CMakeLists.txt
:

cmake_minimum_required(VERSION 3.25)
project(vcpkg_test)

set(CMAKE_CXX_STANDARD 17)

add_executable(vcpkg_test main.cpp)
## link packages
find_package(OpenGL REQUIRED)
target_link_libraries(vcpkg_test PRIVATE opengl32)

find_package(glfw3 CONFIG REQUIRED)
target_link_libraries(vcpkg_test PRIVATE glfw)

find_package(imgui CONFIG REQUIRED)
target_link_libraries(vcpkg_test PRIVATE imgui::imgui)

运行以下代码

main.cpp

#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 330");

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        ImGui::Begin("My name is window, ImGUI window");
        ImGui::Text("Hello there new born!");
        ImGui::End();
        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());


        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    glfwTerminate();
    return 0;
}

我得到了很多

undefined reference to 'ImGui::xxx'

[ 50%] Linking CXX executable vcpkg_test.exe
CMakeFiles\vcpkg_test.dir/objects.a(main.cpp.obj): In function `main':
E:/Fdisk/programming/vcpkg_test/main.cpp:26: undefined reference to `ImGui::DebugCheckVersionAndDataLayout(char const*, unsigned long long, unsigned long long, unsigned long long, unsigned long long, unsigned long long, unsigned long long)'
E:/Fdisk/programming/vcpkg_test/main.cpp:27: undefined reference to `ImGui::CreateContext(ImFontAtlas*)'
E:/Fdisk/programming/vcpkg_test/main.cpp:28: undefined reference to `ImGui::GetIO()'
E:/Fdisk/programming/vcpkg_test/main.cpp:29: undefined reference to `ImGui_ImplGlfw_InitForOpenGL(GLFWwindow*, bool)'
E:/Fdisk/programming/vcpkg_test/main.cpp:30: undefined reference to `ImGui_ImplOpenGL3_Init(char const*)'
E:/Fdisk/programming/vcpkg_test/main.cpp:38: undefined reference to `ImGui_ImplOpenGL3_NewFrame()'
E:/Fdisk/programming/vcpkg_test/main.cpp:39: undefined reference to `ImGui_ImplGlfw_NewFrame()'
E:/Fdisk/programming/vcpkg_test/main.cpp:40: undefined reference to `ImGui::NewFrame()'
E:/Fdisk/programming/vcpkg_test/main.cpp:42: undefined reference to `ImGui::Begin(char const*, bool*, int)'
E:/Fdisk/programming/vcpkg_test/main.cpp:43: undefined reference to `ImGui::Text(char const*, ...)'
E:/Fdisk/programming/vcpkg_test/main.cpp:44: undefined reference to `ImGui::End()'
E:/Fdisk/programming/vcpkg_test/main.cpp:45: undefined reference to `ImGui::Render()'
E:/Fdisk/programming/vcpkg_test/main.cpp:46: undefined reference to `ImGui::GetDrawData()'
E:/Fdisk/programming/vcpkg_test/main.cpp:46: undefined reference to `ImGui_ImplOpenGL3_RenderDrawData(ImDrawData*)'
E:/Fdisk/programming/vcpkg_test/main.cpp:56: undefined reference to `ImGui_ImplOpenGL3_Shutdown()'
E:/Fdisk/programming/vcpkg_test/main.cpp:57: undefined reference to `ImGui_ImplGlfw_Shutdown()'
E:/Fdisk/programming/vcpkg_test/main.cpp:58: undefined reference to `ImGui::DestroyContext(ImGuiContext*)'
collect2.exe: error: ld returned 1 exit status

我之前直接使用ImGui的源码就可以成功运行

main.cpp

我第一次使用vcpkg和ImGui,但lib好像漏掉了一些东西。我确定

["glfw-binding", "opengl3-binding"]
已安装,因为我可以在
./cmake-build-debug/vcpkg_installed/x64-windows/include
中看到相应的头文件。

那么问题出在哪里?

c++ cmake clion vcpkg imgui
1个回答
0
投票

未定义的引用表明这些库未在您的 CMake 配置中与 vcpkg 正确链接。

可能有多种原因。

  1. 首先,请确保使用

    vcpkg integrate install
    设置安装目录(位于 vcpkg 安装目录)。 在您的
    CMakeLists.txt
    中,您缺少
    CMAKE_TOOLCHAIN_FILE
    变量设置
    set(CMAKE_TOOLCHAIN_FILE "[path to vcpkg]/scripts/buildsystems/vcpkg.cmake")

  2. 确保您使用正确的 vcpkg 三元组。就我而言,我使用的是 MinGW,这要求您在 vcpkg 中使用 MinGW 三元组。使用默认的 x64-windows 会导致链接错误。

© www.soinside.com 2019 - 2024. All rights reserved.