我使用vcpkg manifest模式安装软件包,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
中看到相应的头文件。
有什么问题吗?
1条答案
按热度按时间zfciruhq1#
未定义的引用表明这些库没有在你的CMake配置中通过vcpkg正确链接。
可能有几个原因。
1.首先,确保使用
vcpkg integrate install
设置安装目录(在vcpkg安装目录)。在您的CMakeLists.txt
中,您缺少set(CMAKE_TOOLCHAIN_FILE "[path to vcpkg]/scripts/buildsystems/vcpkg.cmake")
的CMAKE_TOOLCHAIN_FILE
变量设置。1.确保你使用的是正确的vcpkg三元组。在我的例子中,我使用的是MinGW,这需要你在vcpkg中使用MinGW三元组。使用默认的x64-windows会导致链接错误。为了使用MinGW三元组,我添加了
字符串
我的
CMakeLists.txt
在调用project(myproject)
之前。正如注解中所指出的,建议在项目中指定
CMAKE_TOOLCHAIN_FILE
,因此定义vcpkg三元组也可以在项目中定义。为此,我引用了vcpkg documentation of triplets。