0

Currently using visual studio code to create a game using glut for graphics, as included by #include "GL/glut.h" however when I compile it

gcc main.c -o test

I am faced with

C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12\
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0xa77): undefined reference to `glBegin@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0xa90): undefined reference to `glColor3f@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0xb49): undefined reference to `glVertex2d@16'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0xb60): undefined reference to `glVertex2d@16'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0xb68): undefined reference to `glEnd@0'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x54a8): undefined reference to `glEnable@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x54bf): undefined reference to `glBlendFunc@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x55dc): undefined reference to `glClear@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x55eb): undefined reference to `glMatrixMode@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x55f3): undefined reference to `glLoadIdentity@0'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5609): undefined reference to `glScalef@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5634): undefined reference to `gluPerspective@32'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5643): undefined reference to `glMatrixMode@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x564b): undefined reference to `glLoadIdentity@0'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x568a): undefined reference to `gluLookAt@72'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x57d1): undefined reference to `glEnableClientState@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x57e0): undefined reference to `glEnableClientState@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5807): undefined reference to `glVertexPointer@16'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x582e): undefined reference to `glTexCoordPointer@16'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x584e): undefined reference to `glDrawArrays@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5875): undefined reference to `glVertexPointer@16'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x589c): undefined reference to `glTexCoordPointer@16'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x58bc): undefined reference to `glDrawArrays@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x58cb): undefined reference to `glDisable@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x58da): undefined reference to `glBegin@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x58f3): undefined reference to `glColor3f@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x590c): undefined reference to `glVertex3f@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5927): undefined reference to `glVertex3f@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5940): undefined reference to `glVertex3f@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x595b): undefined reference to `glVertex3f@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5974): undefined reference to `glColor3f@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x597c): undefined reference to `glEnd@0'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5988): undefined reference to `glEnable@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5990): undefined reference to `_imp__glutPostRedisplay@0'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5997): undefined reference to `_imp__glutSwapBuffers@0'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x599e): undefined reference to `glFlush@0'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x59f6): undefined reference to `_imp__glutSetCursor@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5a53): undefined reference to `_imp__glutSetCursor@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5b8c): undefined reference to `_imp__glutWarpPointer@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5bef): undefined reference to `_imp__glutGet@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5c05): undefined reference to `_imp__glutGet@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5ccd): undefined reference to `_imp__glutInitDisplayMode@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5ce6): undefined reference to `_imp__glutInitWindowSize@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5cff): undefined reference to `_imp__glutInitWindowPosition@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d1f): undefined reference to `_imp__glutKeyboardFunc@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d30): undefined reference to `_imp__glutKeyboardUpFunc@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d41): undefined reference to `_imp__glutDisplayFunc@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d52): undefined reference to `_imp__glutReshapeFunc@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d63): undefined reference to `glEnable@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d78): undefined reference to `glAlphaFunc@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d87): undefined reference to `_imp__glutSetCursor@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5d98): undefined reference to `glEnable@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5db1): undefined reference to `_imp__glutPassiveMotionFunc@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5dc2): undefined reference to `_imp__glutMotionFunc@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5dd3): undefined reference to `_imp__glutMouseFunc@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5dec): undefined reference to `glPixelStorei@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5e03): undefined reference to `glGenTextures@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5e1b): undefined reference to `glBindTexture@8'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5e6f): undefined reference to `glTexImage2D@36'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5e90): undefined reference to `glTexParameterf@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5eb1): undefined reference to `glTexParameterf@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5ed2): undefined reference to `glTexParameterf@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5ef3): undefined reference to `glTexParameterf@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5f12): undefined reference to `glTexEnvi@12'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5f21): undefined reference to `glEnable@4'
C:\Users\playtech\AppData\Local\Temp\ccgUQvpv.o:main.c:(.text+0x5f29): undefined reference to `_imp__glutMainLoop@0'
collect2.exe: error: ld returned 1 exit status

After googling around for solutions, I have also tried (combinations of -lglut, -lGLU and -lGL)

gcc main.c -o test -lglut

Which returns

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lGL
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lGLU
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lglut

I have opened the file in XCode on my mac however, and while the terminal command of gcc gets faced with a similar clang error, XCode's built in 'build' successfully creates the application.

Additionally, my code used quite a lot of VLA's and C++ didn't work for that, so I'd really like to get this working for C. Visual Studio 2019 didn't like building my solution either, as it didn't like the VLA's.

How can I get gcc to compile this file when it includes glut? Thanks

  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Sep 21 '21 at 03:16

1 Answers1

0

You need to add -L<Directory name> to tell the compiler where to find the library you want to link (glut, GLU and GL), because they cannot be found in standard list of directories.

Refer to the official gcc document: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

-llibrary -l library Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.

The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

The exising question and answer would provide the clear example: How to include needed C library using gcc?

Yanger Yang
  • 121
  • 4