Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu 13.10 C++ OpenGL GLUT - linking issues - undefined reference to `glClearColor'

I am running Ubuntu 13.10 and trying to compile that portion of sample OpenGL code:

#include "GL/freeglut.h"
#include "GL/gl.h"

/* display function - code from:
     http://fly.cc.fer.hr/~unreal/theredbook/chapter01.html
This is the actual usage of the OpenGL library.
The following code is the same for any platform */
void renderFunction()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();
}

/* Main method - main entry point of application
the freeglut library does the window creation work for us,
regardless of the platform. */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("OpenGL - First window demo");
    glutDisplayFunc(renderFunction);
    glutMainLoop();
    return 0;
}

I've used Eclipse to build the project, however it fails on linking level:

Building target: opengl_test
Invoking: GCC C++ Linker
g++ -L/usr/lib -L/usr/lib/fglrx -L/usr/lib/x86_64-linux-gnu -L/usr/lib/i386-linux-gnu/mesa -L/usr/lib/x86_64-linux-gnu/mesa -o "opengl_test"  ./src/opengl_test.o   
./src/opengl_test.o: In function `renderFunction()':
/var/www/opengl_test/Debug/../src/opengl_test.cpp:10: undefined reference to `glClearColor'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:11: undefined reference to `glClear'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:12: undefined reference to `glColor3f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:13: undefined reference to `glOrtho'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:14: undefined reference to `glBegin'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:15: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:16: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:17: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:18: undefined reference to `glVertex2f'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:19: undefined reference to `glEnd'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:20: undefined reference to `glFlush'
./src/opengl_test.o: In function `main':
/var/www/opengl_test/Debug/../src/opengl_test.cpp:28: undefined reference to `glutInit'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:29: undefined reference to `glutInitDisplayMode'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:30: undefined reference to `glutInitWindowSize'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:31: undefined reference to `glutInitWindowPosition'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:32: undefined reference to `glutCreateWindow'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:33: undefined reference to `glutDisplayFunc'
/var/www/opengl_test/Debug/../src/opengl_test.cpp:34: undefined reference to `glutMainLoop'
collect2: error: ld returned 1 exit status
make: *** [opengl_test] Error 1

... so I've tried G++:

g++ -lGL -lglut opengl_test.cpp  -o test

However it gives pretty much the same output as Eclipse one.

I thought that maybe I need symlink necessary libs manually to /usr/lib. I've searched for libs:

$ sudo apt-file search libGL.so
fglrx: /usr/lib/fglrx/libGL.so
fglrx: /usr/lib/fglrx/libGL.so.1
fglrx: /usr/lib/fglrx/libGL.so.1.2
fglrx: /usr/lib32/fglrx/libGL.so.1.2
fglrx-updates: /usr/lib/fglrx/libGL.so
fglrx-updates: /usr/lib/fglrx/libGL.so.1
fglrx-updates: /usr/lib/fglrx/libGL.so.1.2
fglrx-updates: /usr/lib32/fglrx/libGL.so.1.2
libgl1-mesa-dev: /usr/lib/x86_64-linux-gnu/libGL.so
libgl1-mesa-dev: /usr/lib/x86_64-linux-gnu/mesa/libGL.so
libgl1-mesa-glx: /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1
libgl1-mesa-glx: /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
libgl1-mesa-glx-dbg: /usr/lib/debug/usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
nvidia-173: /usr/lib/nvidia-173/libGL.so
nvidia-173: /usr/lib/nvidia-173/libGL.so.1
nvidia-173: /usr/lib/nvidia-173/libGL.so.173.14.37
nvidia-173: /usr/lib32/nvidia-173/libGL.so
nvidia-173: /usr/lib32/nvidia-173/libGL.so.1
nvidia-173: /usr/lib32/nvidia-173/libGL.so.173.14.37
nvidia-304: /usr/lib/nvidia-304/libGL.so
nvidia-304: /usr/lib/nvidia-304/libGL.so.1
nvidia-304: /usr/lib/nvidia-304/libGL.so.304.88
nvidia-304: /usr/lib32/nvidia-304/libGL.so
nvidia-304: /usr/lib32/nvidia-304/libGL.so.1
nvidia-304: /usr/lib32/nvidia-304/libGL.so.304.88
nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so
nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so.1
nvidia-304-updates: /usr/lib/nvidia-304-updates/libGL.so.304.108
nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so
nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so.1
nvidia-304-updates: /usr/lib32/nvidia-304-updates/libGL.so.304.108
nvidia-319: /usr/lib/nvidia-319/libGL.so
nvidia-319: /usr/lib/nvidia-319/libGL.so.1
nvidia-319: /usr/lib/nvidia-319/libGL.so.319.32
nvidia-319: /usr/lib32/nvidia-319/libGL.so
nvidia-319: /usr/lib32/nvidia-319/libGL.so.1
nvidia-319: /usr/lib32/nvidia-319/libGL.so.319.32
nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so
nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so.1
nvidia-319-updates: /usr/lib/nvidia-319-updates/libGL.so.319.60
nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so
nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so.1
nvidia-319-updates: /usr/lib32/nvidia-319-updates/libGL.so.319.60
primus-libs: /usr/lib/x86_64-linux-gnu/primus/libGL.so.1

and

$ sudo apt-file search libglut.so
freeglut3: /usr/lib/x86_64-linux-gnu/libglut.so.3
freeglut3: /usr/lib/x86_64-linux-gnu/libglut.so.3.9.0
freeglut3-dev: /usr/lib/x86_64-linux-gnu/libglut.so

Made symlinks:

sudo ln -s /usr/lib/fglrx/libGL.so /usr/lib/libGL.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libglut.so /usr/lib/libglut.so

However it still did not help. The same linking issue appeared.

  1. How do I debug that issue?
  2. How do I know which lib to choose and link them to /usr/lib? I chose fglrx, because my GPU drivers are running fglrx.
  3. Do I need each time to link library manually from /usr/lib..../somewhere to /usr/lib?
like image 227
Tomasz Raganowicz Avatar asked Jan 20 '14 21:01

Tomasz Raganowicz


1 Answers

Try putting your libraries after the source file:

g++ opengl_test.cpp -lGL -lglut

To get Eclipse working you need to change so called "command line pattern". I am not Eclipse user actually, but try to check settings here: C/C++ Build -> Settings -> Cross G++ Linker, or some other Linker-related settings.

Clarification: current ld defaults to link with --as-needed flag. In this mode before linking any library ld checks that some symbols from this library needed somewhere. ld perform this checks in order, so if you link something before your source file this library gets excluded. This behaviour can be disabled using -Wl,--no-as-needed gcc flag.

From man ld:

--as-needed
--no-as-needed
    This option affects ELF DT_NEEDED tags for dynamic  libraries  men-
    tioned on the command line after the --as-needed option.  Normally,
    the linker will add a DT_NEEDED tag for each dynamic     library  men-
    tioned  on  the  command line, regardless of whether the library is
    actually needed.  --as-needed causes     DT_NEEDED  tags  to  only  be
    emitted for libraries that satisfy some symbol reference from regu-
    lar objects which is undefined at the point that  the  library  was
    linked.  --no-as-needed restores the default behaviour.
like image 162
vbo Avatar answered Oct 20 '22 21:10

vbo