Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link error when trying to build a simple OpenGL program

This is the OpenGL code:

#include <GL/glut.h>
void display()
{
 glClear(GL_COLOR_BUFFER_BIT);
}

int main(int argc,char **argv)
{
   glutInit(&argc,argv);
   glutCreateWindow("Hello,world!");
   glutDisplayFunc(display);
   glutMainLoop();
}

The error messages are:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/zh/workspace/OpenGL/CppApplication_1'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cppapplication_1
make[2]: Entering directory `/home/zh/workspace/OpenGL/CppApplication_1'
mkdir -p dist/Debug/GNU-Linux-x86
g++ -lglut -lGLU -lGL -lGLEW    -o dist/Debug/GNU-Linux-x86/cppapplication_1 build/Debug/GNU-Linux-x86/main.o -L/usr/lib/x86_64-linux-gnu -Wl,-rpath,/usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/libglut.so /usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGLEW.so /usr/lib/x86_64-linux-gnu/libGLEWmx.so
/usr/bin/ld: build/Debug/GNU-Linux-x86/main.o: undefined reference to symbol 'glClear'
/usr/lib/x86_64-linux-gnu/libGL.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cppapplication_1] Error 1
make[2]: Leaving directory `/home/zh/workspace/OpenGL/CppApplication_1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/zh/workspace/OpenGL/CppApplication_1'
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 124ms)

At first I had thought this is because the OpenGL version installed is too low, but glClear is available from OpenGL 1.0 and exists in all versions (see here). This is version information of the OpenGL on my system. enter image description here

I built the freeglut 2.8.1 and glew 1.10.0 and have them installed in my system: enter image description here

I included the path to the library and specified needed library in the environment: enter image description hereenter image description here

Also, I have read related threads in repository: enter link description here, enter link description here, but they don't help.

I'm worn out and really can not find what I am missing to build such a simple OpenGL code. Could you please tell me how to troubleshoot this problem? Thank you.

The environment I am using are: Ubuntu 14.04 (64 bit)+ NetBeans 8.0

A hint: when I commented the glClear out, the program can be built successfully.

Edit: For those who working on windows 7 and encountering similar linking problem (OpenGL function not found), Vishwanath gowda's answer in this thread may help.

Edit2: We know that Windows supports OpenGL poorly, if you are at the same time using Intel's entrance-level integrated graphics card for which intel's driver provides no additional support of OpenGL, you'll have to make a new Mesa's OpenGL libray according to the guide here. This can be done because OpenGL is independent of hardware so can be implemented solely by software (A book claims so). Be careful to use machine=x86_64 if you are working on 64bit Win7. You can check that by observing the output of dumpbin /headers youropengldll.dll|more. You can also check that the functionality of OpenGL on your windows system is enhanced after that using a software "OpenGL Extension Viewer".

like image 718
user2384994 Avatar asked May 19 '14 03:05

user2384994


2 Answers

I'm able to compile your example program on a 64-bit Ubuntu 14.04 system using this command:

g++ example.c -lGL -lGLU -lGLEW -lglut -o example

The order of the link options is important: libraries have to be specified after the object files that depend on them. From the GCC documentation for the -l option:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

(Strangely, the program compiles without errors on my Debian systems even if I put the -l options first. But not on Ubuntu.)

like image 54
Wyzard Avatar answered Oct 08 '22 05:10

Wyzard


OK, I've found the key: I should explicitly specify one more library file that contains needed OpenGL function: libGL.so, as the following image shows:

enter image description here

Adding a few more lines, this is the output of this simple OpenGL program:

enter image description here

I think this should be a NetBeans-specific problem, maybe not appear on other IDE like eclipse.

edit: Although the above approach works, the key is to set the link option in NetBeans. All we need to do is type "-lGL -lGLU -lGLEW -lglut" in the Project Properties -> Linker -> Additional Options, as the image below shows:

enter image description here

Thank Wyzard for pointing out this.

like image 34
user2384994 Avatar answered Oct 08 '22 07:10

user2384994