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.

Theo Dale
  • 23
  • 3
  • that only works if there is a `libimgui.so` or `libimgui.a` on your linker's search path. Did you compile imgui into a library yourself? Otherwise you need to add the imgui cpp files to your build process, as explained [here](https://github.com/ocornut/imgui#usage) – Botje Dec 06 '21 at 19:11
  • I think you want to remove `-limgui` – drescherjm Dec 06 '21 at 19:27
  • @Botje it says i dont have to compile the library i can just use the code blocks in the .cpp files wich i have already imported into my project file, and when i do try to compile it with 'g++ -lstdc++ imgui*.h imgui*.cpp' it just spits out the error '/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x20): undefined reference to `main'' – Theo Dale Dec 07 '21 at 15:01

1 Answers1

2

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

Botje
  • 26,269
  • 3
  • 31
  • 41
  • running 'g++ main.cpp -o main include/imgui/imgui*.cpp include/imgui/backends/imgui_impl_glfw.cpp include/imgui/backends/imgui_impl_opengl3.cpp -I include/imgui -I include/imgui/backends -lglfw -lGL -ldl' did the trick. thank you so much, ive been working on this for days – Theo Dale Dec 07 '21 at 16:10
  • The next step is obviously to use a proper build system such as CMake or Make. Or at least compile imgui into a static library so you don't recompile it every time you make a change to your application. – Botje Dec 07 '21 at 18:28