Search code examples
c++glfwimgui

failure to compile imgui, glfw, opengl on linux with gcc 11.2


I've recently started coding with c++ and the project that im currently on requires imgui. so i set up the .h and .cpp libraries in a folder called "include" in the same folder as the source code. I'm currently trying to run the cpp in https://github.com/ocornut/imgui/tree/master/examples/example_glfw_opengl3 and compile it with gcc using gcc imgui.cpp -lstdc++ -lglfw -lGL -limgui but i just get

/usr/bin/ld: cannot find -limgui
collect2: error: ld returned 1 exit status

yes i know there is a make file in the link but im using the file in another folder.


Solution

  • Assuming you downloaded imgui to a place called $IMGUI_DIR and the file that contains your main function is main.cpp, your compile commandline should look like the following: (the \ are just there to break up the command)

    g++ main.cpp -o main \
    $IMGUI_DIR/imgui*.cpp $IMGUI_DIR/backends/imgui_impl_glfw.cpp $IMGUI_DIR/backends/imgui_impl_opengl3.cpp \
    -I $IMGUI_DIR -I $IMGUI_DIR/backends \
    -lglfw -lGL
    

    In order, you tell the compiler:

    • Where your code is and where to put the output
    • What Imgui code to include, namely the core library and the two backends you want to use
    • Where Imgui code should look for its headers

    If all this sounds like a lot, know that you can simply build the example you linked and override IMGUI_DIR at compile time with make IMGUI_DIR=/path/to/imgui