Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: undefined reference to `glutInit'

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2010
    Location
    US
    Posts
    10

    undefined reference to `glutInit'

    I am an amateur at c, c++ programming.

    I guess am trying to stay with C++ because all of the other parts of this project code is C++.

    The code below compiles and runs as a c program.

    When I copied the code into a Code Blocks C++ console project I get "undefined reference to `glutInit'" and all other functions in main.

    obj/Debug/main.o||In function `main':|
    /home/gary/Documents/screen1/main.cpp|138|undefined reference to `glutInit'|
    /home/gary/Documents/screen1/main.cpp|139|undefined reference to `glutInitDisplayMode'|
    /home/gary/Documents/screen1/main.cpp|140|undefined reference to `glutInitWindowSize'|
    /home/gary/Documents/screen1/main.cpp|141|undefined reference to `glutInitWindowPosition'|
    /home/gary/Documents/screen1/main.cpp|142|undefined reference to `glutCreateWindow'|
    /home/gary/Documents/screen1/main.cpp|144|undefined reference to `glutDisplayFunc'|
    /home/gary/Documents/screen1/main.cpp|145|undefined reference to `glutMainLoop'|
    ||=== Build finished: 7 errors, 0 warnings ===|


    both files are below.

    I have add header files to the C++ file trying to resolve the problem.

    1.) How do I resolve error?
    2.) Why does it work in C and not C++?

    ----------------------C file---------------------------

    #include <GL/glut.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <GL/glx.h> /* this includes the necessary X headers */
    #include <GL/gl.h>
    #include <X11/X.h> /* X11 constant (e.g. TrueColor) */
    #include <X11/keysym.h>
    #include <math.h>

    using namespace std;
    const float DEG2RAD = 3.14159/180;

    void disp_rect(float x1, float y1, float x2, float y2)
    {
    glColor3f (1.0, 0.0, 0.0);
    /*glRectf(50.0, 50.0, 800.0, 600.0);*/
    glRectf(x1,y1,x2,y2);
    return;
    }

    void disp_line(float x1, float y1, float x2, float y2)
    {
    glColor3f (0.0, 0.0, 1.0);
    glBegin(GL_LINES);
    glVertex3f(x1,y1,0.0);
    glVertex3f(x2,y2,0.0);
    glEnd();
    return;
    }
    void disp_point(float x1, float y1)
    {
    glColor3f (0.0, 1.0, 0.0);
    glBegin(GL_POINTS);
    glVertex3f(x1,y1,0.0);

    glEnd();
    return;
    }


    void G02drawCircle(float radius)
    {
    glBegin(GL_LINES);
    // glBegin(GL_LINE_LOOP);
    int i;
    for (i=0; i < 360; i++)
    {
    float degInRad = i*DEG2RAD;
    glVertex2f((cos(degInRad)*radius)+200.00,(sin(degI nRad)*radius)+200);
    }

    glEnd();
    }

    void G03_arc (float x1, float y1, float x2, float y2, float I, float J)
    {
    float x_offset;
    float y_offset;
    float radius;
    x_offset = x1 + I;
    y_offset = y1 + J;

    return;

    }

    void display(void)
    {
    /* clear all pixels */
    glClear (GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    glVertex2f(820.0,400.0);
    glEnd();

    disp_rect(300.0,50.0,800.0,600.0);

    disp_line(20.0,30.0,70.0,400.0);

    int i;
    for (i=0;i<300;i=i+1)
    {
    disp_point(100.0 + i,200.0 +i);
    }

    G02drawCircle(100);

    G03_arc (104.8569, 2.7599, 104.8372, 2.7796, -.0197, 0.0);
    /* don't wait! float x1, float y1, float x2, float y2, float I, float
    * start processing buffered OpenGL routines
    */

    glFlush ();
    }



    void init (void)
    {
    /* select clearing (background) color */
    glClearColor (0.0, 0.0, 0.0, 0.0);

    /* initialize viewing values */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,1000, 500);

    glOrtho(00.0,1000.0, 00.0, 800.0, -1.0, 1.0);

    }

    /*
    * Declare initial window size, position, and display mode
    * (single buffer and RGBA). Open window with "hello"
    * in its title bar. Call initialization routines.
    * Register callback function to display graphics.
    * Enter main loop and process events.
    */
    int main(int argc, char** argv)
    {
    glutInit(&amp;argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (1000, 800);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0; /* ISO C requires main to return int. */
    }


    ------------------------end C file that works-----------------------

    ------------------------c++ file that will not run------------

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <stdio.h>
    #include <string.h>
    #include <GL/glut.h>

    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <GL/glx.h> /* this includes the necessary X headers */
    #include <GL/gl.h>
    #include <stdlib.h>
    #include <X11/X.h> /* X11 constant (e.g. TrueColor) */
    #include <X11/keysym.h>
    #include <math.h>

    using namespace std;
    const float DEG2RAD = 3.14159/180;
    void glutInit(int *argcp, char **argv);
    void disp_rect(float x1, float y1, float x2, float y2)
    {
    glColor3f (1.0, 0.0, 0.0);
    /*glRectf(50.0, 50.0, 800.0, 600.0);*/
    glRectf(x1,y1,x2,y2);
    return;
    }

    void disp_line(float x1, float y1, float x2, float y2)
    {
    glColor3f (0.0, 0.0, 1.0);
    glBegin(GL_LINES);
    glVertex3f(x1,y1,0.0);
    glVertex3f(x2,y2,0.0);
    glEnd();
    return;
    }
    void disp_point(float x1, float y1)
    {
    glColor3f (0.0, 1.0, 0.0);
    glBegin(GL_POINTS);
    glVertex3f(x1,y1,0.0);

    glEnd();
    return;
    }


    void G02drawCircle(float radius)
    {
    glBegin(GL_LINES);
    // glBegin(GL_LINE_LOOP);
    int i;
    for (i=0; i < 360; i++)
    {
    float degInRad = i*DEG2RAD;
    glVertex2f((cos(degInRad)*radius)+200.00,(sin(degI nRad)*radius)+200);
    }

    glEnd();
    }

    void G03_arc (float x1, float y1, float x2, float y2, float I, float J)
    {
    float x_offset;
    float y_offset;
    float y_offseta;

    float radius;
    x_offset = x1 + I;
    y_offset = y1 + J;
    y_offseta = y2 +x2 + J;
    radius = 1;
    if (I == 0)
    {
    cout << radius ;
    }

    cout << radius ;
    return;

    }

    void display(void)
    {
    /* clear all pixels */
    glClear (GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    glVertex2f(820.0,400.0);
    glEnd();

    disp_rect(300.0,50.0,800.0,600.0);

    disp_line(20.0,30.0,70.0,400.0);

    int i;
    for (i=0;i<300;i=i+1)
    {
    disp_point(100.0 + i,200.0 +i);
    }

    G02drawCircle(100);

    G03_arc (104.8569, 2.7599, 104.8372, 2.7796, -.0197, 0.0);
    /* don't wait! float x1, float y1, float x2, float y2, float I, float
    * start processing buffered OpenGL routines
    */

    glFlush ();
    }


    void init (void)
    {
    /* select clearing (background) color */
    glClearColor (0.0, 0.0, 0.0, 0.0);

    /* initialize viewing values */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,1000, 500);

    glOrtho(00.0,1000.0, 00.0, 800.0, -1.0, 1.0);

    }

    /*
    * Declare initial window size, position, and display mode
    * (single buffer and RGBA). Open window with "hello"
    * in its title bar. Call initialization routines.
    * Register callback function to display graphics.
    * Enter main loop and process events.
    */
    int main(int argc, char** argv)
    {
    glutInit(&amp;argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (1000, 800);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0; /* ISO C requires main to return int. */
    }

  2. #2
    Intern Contributor
    Join Date
    Sep 2010
    Posts
    74

    Re: undefined reference to `glutInit'

    If you're using Code::Blocks, you have to make sure you link with the right libraries in Project->Build Options. It seems you don't link with any glut library at the moment.

  3. #3
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    2,882

    Re: undefined reference to `glutInit'

    Quote Originally Posted by Gary Bennett
    When I copied the code into a Code Blocks C++ console project I get "undefined reference to `glutInit'" and all other functions in main.

    obj/Debug/main.o||In function `main':|
    /home/gary/Documents/screen1/main.cpp|138|undefined reference to `glutInit'|
    /home/gary/Documents/screen1/main.cpp|139|undefined reference to `glutInitDisplayMode'|
    /home/gary/Documents/screen1/main.cpp|140|undefined reference to `glutInitWindowSize'|
    /home/gary/Documents/screen1/main.cpp|141|undefined reference to `glutInitWindowPosition'|
    ...

    1.) How do I resolve error?
    2.) Why does it work in C and not C++?
    You need to get -lglut -lGLU -lGL on the link line. For a one-file app, you'd do something like this:

    g++ -o app app.cpp -lglut -lGLU -lGL

    -lglut (for instance) merely tried to find the file libglut.so in the system library search path. Often this is installed in /usr/lib64 (64-bit) or /usr/lib (32-bit).

    Oftentimes, -lglut will be sufficient (as the dependencies cascade if the libglut library was built properly). But if not, add the other two.

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2010
    Location
    US
    Posts
    10

    Re: undefined reference to `glutInit'

    Thank you for replying. Between your response and Dark Photon's reply the problem was resolved by adding a linker location to libglut.a and libGLU.so

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    7
    How to set these linker options in codeblocks? i.e -lglut -lGLU -lGL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •