8

I wrote this code:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glx.h>    
#include <GL/gl.h>
#include <GL/glut.h>


void init()
{
    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,200.0,0.0,150.0);
 }

 void lineSegment()
 {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0);
        glBegin(GL_TRIANGLES);
            glVertex2i(40,120);
            glVertex2i(40,20);
            glVertex2i(80,20);
        glEnd();
    glFlush();
 }
 int main()
 {
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(50,100);
    glutInitWindowSize(400,300);
    glutCreateWindow("An Example OpenGL....");
    init();
    glutDisplayFunc(lineSegment);
    glutMainLoop();
    return 0;
 }

and this is my error.

funfullson@funfullson:~$ gcc gl.cpp 
/tmp/cchfRGT2.o: In function `init()':
gl.cpp:(.text+0x2a): undefined reference to `glClearColor'
gl.cpp:(.text+0x36): undefined reference to `glMatrixMode'
gl.cpp:(.text+0x5a): undefined reference to `gluOrtho2D'
/tmp/cchfRGT2.o: In function `lineSegment()':
gl.cpp:(.text+0x6e): undefined reference to `glClear'
gl.cpp:(.text+0x8d): undefined reference to `glColor3f'
gl.cpp:(.text+0x99): undefined reference to `glBegin'
gl.cpp:(.text+0xad): undefined reference to `glVertex2i'
gl.cpp:(.text+0xc1): undefined reference to `glVertex2i'
gl.cpp:(.text+0xd5): undefined reference to `glVertex2i'
gl.cpp:(.text+0xda): undefined reference to `glEnd'
gl.cpp:(.text+0xdf): undefined reference to `glFlush'
/tmp/cchfRGT2.o: In function `main':
gl.cpp:(.text+0xf6): undefined reference to `glutInitDisplayMode'
gl.cpp:(.text+0x10a): undefined reference to `glutInitWindowPosition'
gl.cpp:(.text+0x11e): undefined reference to `glutInitWindowSize'
gl.cpp:(.text+0x12a): undefined reference to `glutCreateWindow'
gl.cpp:(.text+0x13b): undefined reference to `glutDisplayFunc'
gl.cpp:(.text+0x140): undefined reference to `glutMainLoop'
/tmp/cchfRGT2.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

I dont know what I have to do.please learn me how to do it.

mloskot
  • 37,086
  • 11
  • 109
  • 136
funfullson
  • 107
  • 1
  • 1
  • 4
  • I'm beginning getting tired of this, but anyway: Projection matrix and Viewport setup belong in the display routine, not some obscure "init" function. -- The solution to your actual problem has been given in an answer already. – datenwolf Mar 13 '11 at 12:39

3 Answers3

22

First, you should use g++ to compile C++ code, not gcc.

Then, you have to link to the OpenGL libraries when building your program:

$ g++ gl.cpp -o gl -lGL -lGLU -lglut

The command line above will produce an executable file called gl in the current directory. Without -o, the resulting executable is called a.out (on my platform), which is probably not what you want.

(Note that according to cipher's comment below, source files have to precede library options in order for g++ to compile them.)

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • He should `mv gl.cpp gl.c` because theres no C++ whatsoever being used. – alternative Mar 13 '11 at 12:32
  • 1
    @mathepic, you're probably right, but I can't assume that: maybe the `cpp` extension is actually meaningful and the code will be full of classes in a week ;) – Frédéric Hamidi Mar 13 '11 at 12:35
  • @FrédéricHamidi : It looks like you have to place the file in front of any `-l` options, just as @warl0ck said in his answer! – cipher Mar 04 '14 at 15:55
  • @cipher, really? What makes you think that? This can be true with object files (they should be mentioned before libraries so all required symbols can be pulled) but not with source files, at least in my experience. – Frédéric Hamidi Mar 04 '14 at 16:01
  • @FrédéricHamidi Well, That just so happened right now, i wasn't able to compile with the source file at the end, but was able to compile with the source file before -l . :) – cipher Mar 04 '14 at 16:05
  • 1
    @cipher, interesting, I don't have a `g++` handy right now to double-check this, but I do believe you. I will update my answer accordingly. Thanks for the heads-up :) – Frédéric Hamidi Mar 04 '14 at 16:09
  • Worth noting that `-lGL -lGLU -lglut` works right out of the box on Linux at least. I just spent 30 minutes trying to download OpenGL searching for the folders `GL/`, `GLU/`, and `glut/`. – mazunki Apr 25 '20 at 14:57
8

You will have to place the file in front of any -l options , here for an example:

g++ X.cpp -lX11 -lGL -lGLU -lglut -g -Wall -O2 -o X

Also make sure the following packages is already present:

sudo apt-get install mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev

And ensure glx module is loaded in Xorg

Section "Module"
    Load "glx"
EndSection

Good luck with OpenGL programming !

daisy
  • 22,498
  • 29
  • 129
  • 265
0

For Compilation

g++ filename.cpp -lGL -lGLU -lglut

For Execution

./a.out
am2505
  • 2,194
  • 15
  • 19