5

I'm using Ubuntu and tried using synaptic to install everything that had the word "GLUT" in it and also SDL and opengl . But still a simple program fails to compile . It shows this :

opengl1.cpp:(.text+0xe): undefined reference to `glClear'
opengl1.cpp:(.text+0x1a): undefined reference to `glBegin'
opengl1.cpp:(.text+0x2e): undefined reference to `glVertex2i'
opengl1.cpp:(.text+0x33): undefined reference to `glEnd'
opengl1.cpp:(.text+0x38): undefined reference to `glFlush'
/tmp/ccnwQeLu.o: In function `MyInit()':
opengl1.cpp:(.text+0x4c): undefined reference to `glGetString'
opengl1.cpp:(.text+0x57): undefined reference to `std::cout'
opengl1.cpp:(.text+0x5c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)'
opengl1.cpp:(.text+0x6c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
opengl1.cpp:(.text+0x78): undefined reference to `glGetString'
opengl1.cpp:(.text+0x83): undefined reference to `std::cout'
opengl1.cpp:(.text+0x88): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)'
opengl1.cpp:(.text+0x98): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
opengl1.cpp:(.text+0xc0): undefined reference to `glClearColor'
opengl1.cpp:(.text+0xdf): undefined reference to `glColor3f'
opengl1.cpp:(.text+0xec): undefined reference to `glPointSize'
opengl1.cpp:(.text+0xf8): undefined reference to `glMatrixMode'
opengl1.cpp:(.text+0xfd): undefined reference to `glLoadIdentity'
opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D'
/tmp/ccnwQeLu.o: In function `main':
opengl1.cpp:(.text+0x14a): undefined reference to `glutInit'
opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode'
opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize'
opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition'
opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow'
opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc'
opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop'
/tmp/ccnwQeLu.o: In function `__static_initialization_and_destruction_0(int, int)':
opengl1.cpp:(.text+0x1cb): undefined reference to `std::ios_base::Init::Init()'
opengl1.cpp:(.text+0x1d0): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccnwQeLu.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

If I use g++ instead of gcc I get this:

vim opebgl1.cpp
g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm
 and then get this :
/tmp/ccCJBuIl.o: In function `MyInit()':
opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D'
/tmp/ccCJBuIl.o: In function `main':
opengl1.cpp:(.text+0x14a): undefined reference to `glutInit'
opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode'
opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize'
opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition'
opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow'
opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc'
opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status

So what do I really need to start working with opengl in Ubuntu?

6
  • Are you linking in all the appropriate libraries when building your application? Commented Mar 23, 2011 at 19:56
  • I included these header files :#include <X11/Xlib.h> #include <GL/glut.h> Commented Mar 23, 2011 at 19:57
  • Also, are you building with g++ or gcc? Commented Mar 23, 2011 at 19:57
  • Including header files is only half of what you need to do. That allows the program to compile but not link. The command that ultimately creates your application (or library) should link to the libraries you used otherwise it won't know where to get all these symbols from. Post your build commands and we can give you some pointers. Commented Mar 23, 2011 at 19:59
  • Use g++ to build C++ source. You'll find the errors relating to things like std::cout go away then. g++ is really just a wrapper around gcc that automatically links to the standard C++ libraries for you. If you use gcc you have to do it manually. It's far easier to use g++. Commented Mar 23, 2011 at 20:01

4 Answers 4

8

Add "-lstdc++ -lGL" to your linker flags. Or try to compile it like this:

g++ opengl1.cpp -o opengl1 -lGL -lGLU -lc -lm

(edit: added -lGLU, removed -lstdc++)

Sign up to request clarification or add additional context in comments.

6 Comments

Now im getting /tmp/ccCJBuIl.o: In function MyInit()': opengl1.cpp:(.text+0x12d): undefined reference to gluOrtho2D' /tmp/ccCJBuIl.o: In function main': opengl1.cpp:(.text+0x14a): undefined reference to glutInit' opengl1.cpp:(.text+0x156): undefined reference to glutInitDisplayMode' opengl1.cpp:(.text+0x16d): undefined reference to opengl1.cpp:(.text+0x181): undefined reference to glutInitWindowPosition' opengl1.cpp:(.text+0x19e): undefined reference to glutDisplayFunc' opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop' collect2: ld returned 1 exit status
@user: Also add -lglut and -lGLU to the left of -lGL.
Why would you need -lstdc++ if you are using g++? It appears the problem is that @user553492 is using gcc by mistake.
@pelya: -lstdc++ is implicit when you compile/link with g++ (unless you explicitly disable it)
The errors seem to have gon with using g++ . The only problem now is that the program compiles but when I run it , it says command not found .
|
3

You should link it with glut and GLU as well:

g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm -lglut -lGLU

Files ended with .cpp must be compiled with g++. The rest of the errors are related to the linking process, and they should not happen if you build your application with the command I suggested above. If they do, make sure you have installed libglut and libglu.

Comments

2

Make sure you are using g++ to build your C++ files to avoid the linker errors pertaining to the C++ standard library.

To resolve the GLUT symbols you probably just need to add -lglut to your final build command.

Comments

1

While compiling the code you must mention some parameters according to your includes. You already used -lGL, -lm etc. To use gluOrtho2D the parameter to be used is -lGLU and for other functions that begin with glut use -lglut

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.