Search code examples
c++cmakecmakelists-options

"undefined reference to .." error after make command


I am trying to include some libraries i downloaded using vcpkg. I want to use all of them from vcpkg instead of system libraries.

I am able to run cmake .. command in build directory. It is successful. Then i run make command but i am getting lots of "undefined reference to" error for all libraries i linked.

Here is CmakeLists.txt:

set(CMAKE_TOOLCHAIN_FILE "/home/ubuntu-user/Desktop/flight-simulator/vcpkg/scripts/buildsystems/vcpkg.cmake")

set(PROJECT_NAME Simulator)

cmake_minimum_required(VERSION 3.12)
project(${PROJECT_NAME})

##OPENGL
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})

##GLAD
find_path(GLAD_INCLUDE_DIR  glad/glad.h)
find_library(GLAD_LIBRARY glad)
#message(${GLAD_INCLUDE_DIR})

#ASSIMP
find_library(ASSIMP_LIBRARY assimp)

#IMGUI
set(IMGUI_DIR ./libs/imgui-implot)
add_library(imgui STATIC 
    ${IMGUI_DIR}/imgui_impl_glfw.cpp 
    ${IMGUI_DIR}/imgui.cpp
)

#SOURCES
FILE(GLOB SOURCES
        "src/Data.cpp" 
        "src/Frames.cpp" 
        "src/Model.cpp" 
        "src/main.cpp" 
        )


#EXECUTABLE
ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES})


target_include_directories(${PROJECT_NAME} PRIVATE ${IMGUI_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${GLAD_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME}  imgui ${OPENGL_LIBRARIES} assimp ${GLAD_LIBRARY})

Sample error log:


/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1535): undefined reference to `glfwSetCursor'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x154e): undefined reference to `glfwSetInputMode'
/usr/bin/ld: libimgui.a(imgui_impl_glfw.cpp.o): in function `ImGui_ImplGlfw_UpdateGamepads()':
imgui_impl_glfw.cpp:(.text+0x15e8): undefined reference to `glfwGetGamepadState'
/usr/bin/ld: libimgui.a(imgui_impl_glfw.cpp.o): in function `ImGui_ImplGlfw_NewFrame()':
imgui_impl_glfw.cpp:(.text+0x1bb5): undefined reference to `glfwGetWindowSize'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1bcf): undefined reference to `glfwGetFramebufferSize'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1c4a): undefined reference to `glfwGetTime'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiStyle::ImGuiStyle()':
imgui.cpp:(.text+0xe23): undefined reference to `ImGui::StyleColorsDark(ImGuiStyle*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImBezierCubicClosestPoint(ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, int)':
imgui.cpp:(.text+0x2614): undefined reference to `ImBezierCubicCalc(ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, float)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiTextFilter::Draw(char const*, float)':
imgui.cpp:(.text+0x564d): undefined reference to `ImGui::InputText(char const*, char*, unsigned long, int, int (*)(ImGuiInputTextCallbackData*), void*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiListClipper_SeekCursorAndSetupPrevLine(float, float)':
imgui.cpp:(.text+0x652f): undefined reference to `ImGui::TableEndRow(ImGuiTable*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiListClipper::Begin(int, float)':

I tried writing add_library instead of add_executable and make completed successfully. But then i don't have any executable output.


Solution

    1. You are using imgui-plot which still requires ImGUI environment. ImGUI Plot Docs here. ImGUI example for GLFW3 and OpenGL3 here
    2. You didn't link GLFW.
    target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} glfw3 imgui assimp ${GLAD_LIBRARY})