Undefined references in OpenGL

  • When I try building an OpenGL program I always get these "undefined reference" errors:
    debug/main.o: In function `glutInit_ATEXIT_HACK': C:/glut/GL/glut.h:486: undefined reference to `_imp____glutInitWithExit' debug/main.o: In function `glutCreateWindow_ATEXIT_HACK': C:/glut/GL/glut.h:503: undefined reference to `_imp____glutCreateWindowWithExit' debug/main.o: In function `glutCreateMenu_ATEXIT_HACK': C:/glut/GL/glut.h:549: undefined reference to `_imp____glutCreateMenuWithExit' collect2.exe: error: ld returned 1 exit status mingw32-make[1]: *** [Makefile.Debug:75: debug/Height.exe] Error 1 mingw32-make[1]: Leaving directory 'C:/Users/Alis/Documents/build-Height-Desktop_Qt_5_13_0_MinGW_32_bit-Debug' mingw32-make: *** [Makefile:38: debug] Error 2 16:14:34: The process "C:\Qt\Qt5.13.0\Tools\mingw730_32\bin\mingw32-make.exe" exited with code 2.
    I linked my files like this:
    LIBS += -lopengl32
    LIBS += -lglut -lGLUT -m32 -lglu32 -lgdi32 -lfreeglut
    Then I right-clicked my project folder and pressed "Add Library" to link my .lib file. What am I doing wrong?

  • Lifetime Qt Champion

    Try adding
    before your first glut.h include.

    Its a shot in the dark, however, it seems to find all the other functions so i dont think
    its just due not finding the libs to link with.

  • I forgot to mention that I have already added that line. Before adding it there were 8 of these errors, now they are only 4

  • Moderators

    -lglut -lGLUT -lfreeglut

    First of all glut and GLUT are the same so that's redundant. Second, glut and freeglut are two different libraries. Which one are you actually using? Third, if you're using glut then the lib is (usually) called glut32, not glut, so check what the name of the .lib or .a file actually is.
    Next, you need to specify the path to the library ie. LIBS += -Lpath_to_glut_or_freeglut_lib_dir. Last but not least - after you make changes to the .pro file make sure you re-run qmake (menu Build - >Run qmake), otherwise those changes might not get picked up.

Log in to reply