6

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 here enter 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".

Community
  • 1
  • 1
user2384994
  • 1,759
  • 4
  • 24
  • 29
  • Why the `-rpath` and the explicit references to files like `/usr/lib/x86_64-linux-gnu/libglut.so`? The `-lglut -lGLU -lGL -lGLEW` should be all you need for linking against OpenGL libraries. – Wyzard May 19 '14 at 03:27
  • Also, you shouldn't need to compile freeglut and GLEW yourself; Ubuntu has them packaged (as freeglut3-dev and libglew-dev, respectively). – Wyzard May 19 '14 at 03:27
  • @Wyzard The purpose of explicit reference to these library files is to tell linker where to find them. If I remove them, more errors come out such as "undefined reference to `glutInit'" which originally does not appear. In addition, I have installed freeglut3-dev and libglew-dev in Ubuntu Software Center. There is a green tick above the icon. – user2384994 May 19 '14 at 03:35
  • 1
    You shouldn't have to tell the linker where to find them. The `-lGL` tells the linker to look for a `libGL.so` in the system's default library directories, which includes `/usr/lib/x86_64-linux-gnu` on a 64-bit Ubuntu system. There's no need for explicit paths to tell the system linker where to find a system library. – Wyzard May 19 '14 at 03:39
  • @Wyzard I have removed them and more errors come out such as "undefined reference to `glutInit'", which originally does not appear if I told linker where to locate them by explicitly specifying library files. – user2384994 May 19 '14 at 03:43
  • BTW, you shouldn't link both GLEW and GLEWmx; they're different builds of the same library. Unless you're using multiple GL contexts (an advanced thing to do), just use the regular GLEW library. – Wyzard May 19 '14 at 04:11

2 Answers2

13

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.)

Wyzard
  • 33,849
  • 3
  • 67
  • 87
2

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.

user2384994
  • 1,759
  • 4
  • 24
  • 29
  • You shouldn't be specifying those explicit `.so` files at all; you just need to configure NetBeans to put the `-l` options in the right place. See my answer. – Wyzard May 19 '14 at 04:06
  • @Wyzard Yes, you are right. I have editted my answer to reflect your idea. – user2384994 May 19 '14 at 06:34