I use vcpkg manifest mode to install packages, vcpkg.json
:
{
"name": "vcpkgtest",
"version": "1.0.0",
"description": "learn vkpg & glfw & imgui & cmake",
"dependencies": [
"glfw3",
{
"name": "imgui",
"features": ["glfw-binding", "opengl3-binding"]
}
]
}
and installed them succesfully, 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)
Run the following code 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;
}
I get many 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
I've directly used the source code of ImGui before to run the main.cpp
successfully.
It's my first time to use vcpkg and ImGui, but it seems that the lib missed something. I'm sure that ["glfw-binding", "opengl3-binding"]
are installed because I can see the corresponding header files in ./cmake-build-debug/vcpkg_installed/x64-windows/include
.
So what's the problem?