1

I am very new to c++ but i know a couple other languages (ie. python, java, a little C). The problem is when i compile my code and try to open a new ImGui window it gives a "undefined reference" error. I think it is a problem with how i the setup the files. both my main.cpp file and the imgui library are in the same directory and when i try to import the file by using "#include "imgui/imgui.h" " it still gives my the undefined reference error. I have tried to pull all the .h and .cpp files out of the imgui folder and into the same directory as my "Main.cpp" file but it still gives me a undefined reference error. I have read the install instructions where you put the .cpp files into the working directory and it still didn't work and gave me the same error. Any help would be appreciated. --------------------------------------------MyCode--------------------------------------------

#include <iostream>
using namespace std;

#include "imgui/imgui.h"

int main(){
    ImGui::Begin("Window");
    ImGui::End();
}

---------------------------------------------Output---------------------------------------------

/tmp/ccMXHI3r.o: In function `main':
Main.cpp:(.text+0x16): undefined reference to `ImGui::Begin(char const*, bool*, int)'
collect2: error: ld returned 1 exit status

-------------------------------my working directory-----------------------

imgui       // imgui library folder that i downloaded from github
Main.cpp    // my cpp file where my code is
Cooper Keely
  • 11
  • 1
  • 1
  • 3
  • 1
    What build system are you using? Please try to learn about C++ program compilation process. Header files do not matter to a compiler if they can be opened, .cpp files are compiled separately and linked together. Undefined reference usually means you forgot to compile and link a .cpp file. In this case imgui.cpp. – Quimby Sep 17 '20 at 19:06
  • I am using Ubuntu 18.04 LTS and i just checked the ubuntu software center and it dosent have it. if that was what your were talking about. – Cooper Keely Sep 17 '20 at 19:57
  • No, I was asking about how you compile your program. Do you use make, cmake or some IDE like VScode or Eclipse? I believe compiling C++ with foreign packages is really unfriendly to beginners if you come from python or java background with their fancy modules, so getting errors is quite normal. – Quimby Sep 17 '20 at 20:02
  • You probably want to check out this: [https://github.com/ocornut/imgui/blob/c206a193737811193a0b48ef2d35fe028fa0996e/examples/README.txt#L160](https://github.com/ocornut/imgui/blob/c206a193737811193a0b48ef2d35fe028fa0996e/examples/README.txt#L160) – drescherjm Sep 17 '20 at 20:46
  • 1
    Ive been using Vs code with the extensions code runner and microsofts c/c++. – Cooper Keely Sep 17 '20 at 22:20
  • even when i try to compile the given imgui_demo.cpp file it gives me an undefined reference error even though it is in the downloaded folder that came from github. – Cooper Keely Sep 17 '20 at 22:52
  • @CooperKeely Sorry but you are not using Microsoft C++. That error message clearly comes from the g++ compiler. When you start doing 'advanced' stuff like linking with third party libraries then you really have to understand the tools you are working with. – john Nov 22 '22 at 13:44

2 Answers2

2

The problem seem to be a missing linking step, you need to tell your compilation/linking process to link to imgui library.
When you include a header in C/C++ you are just letting the compiler to know the signatures of functions and other declarations. Then you need to let your linker know where to look for the code, and that will be telling it to link to the library.

For example with gcc you can make a compilation + linking with the following call:

gcc foo.c -o foo -ldynLib

that example will compile the source code 'foo.c', will generate an object for that and it will link it with the dynamic library 'dynlib' into the final binary 'foo' (the executable).

in your case it could be something like this:

gcc Main.cpp -o program -limgui

Of course flags and steps will depend on your compiler / linker, also you should check the actual name of the library (imgui is just a guess, as I don't know that library)

Pablo Yaggi
  • 1,061
  • 5
  • 14
0

ImGui is a bizarre minimalist C++ windows library system that requires more than just a linker or compiler flag to sort out.

Here is the github repository: https://github.com/ocornut/imgui

I can't tell if you are using OpenGL, or Vulkan or Windows, or Linux as a few of the many choices that you must make clear if someone can solve your problem for you succinctly.

If you go to the website and look at the instructions it might be simpler just to copy into your directory the imgui files you need and link to them and compile them locally...

daemondave
  • 309
  • 2
  • 12