OpenGL That use gl.h and glu.h unable to build: 'undefined reference to 'gluPerspective' '

76 Views Asked by At

I have created this header (.h file) and C++ file (.cpp) to show rotating object

the header file (CGfxOpenGL.h) :


class CGfxOpenGL

{
private:
    int m_windowWidth;
    int m_windowHeight;

    float m_angle;

public:
    CGfxOpenGL();
    virtual ~CGfxOpenGL();

    bool Init();
    bool Shutdown();

    void SetupProjection(int width, int height);

    void Prepare(float dt);
    void Render();
};

The cpp file (learnopengl2triangleglew.cpp)


#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include "CGfxOpenGL.h"

#include <iostream>

CGfxOpenGL::CGfxOpenGL()
{
}

CGfxOpenGL::~CGfxOpenGL()
{
}

bool CGfxOpenGL::Init()
{
    // clear to black background
    glClearColor(0.0, 0.0, 0.0, 0.0);
    
    m_angle = 0.0f;

    return true;
}

bool CGfxOpenGL::Shutdown()
{
    return true;
}

void CGfxOpenGL::SetupProjection(int width, int height)
{
    if (height == 0)
    {
        height = 1; 
    }
    glViewport(0, 0, width, height); // reset the viewport to new dimensions
    glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
    glLoadIdentity(); // reset projection matrix
    // calculate aspect ratio
    gluPerspective(52.0f,(GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    m_windowWidth = width;
    m_windowHeight = height;
}

void CGfxOpenGL::Prepare(float dt)
{
    m_angle += 0.1f;
}

void CGfxOpenGL::Render()
{
    // clear screen and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    // move back 5 units and rotate about all 3 axes
    glTranslate(0.0, 0.0, -5.0f);
    glRotatef(m_angle, 1.0f, 0.0f, 0.0f);
    glRotatef(m_angle, 0.0f, 1.0f, 0.0f);
    glRotatef(m_angle, 0.0f, 0.0f, 1.0f);

    // lime greenish color
    glColor3f(0.7f, 1.0f, 0.3f);

    // draw the triangle such that the rotation point is in the center
    glBegin(GL_TRIANGLES);
        glVertex3f(1.0f, -1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
    glEnd();
}

the problem is after I type this in terminal:

g++ -o test learnopengl2triangleglew.cpp -lGL -lGLU -ldl

I get: undefined reference to 'main'

My conditions:

  1. I have build and install GL. I have GL/gl.h and GL/glu.h as well.
  2. I have math.h from glm too.
  3. I am using Linux OS.

How should I solve this?

P.S. It is an example file from Beginning OpenGL Game Programming Book.

this is the screenshot of the error (I fix the gluPerspective by adding -lGLU 1

1

There are 1 best solutions below

5
ScriptSorcerer On BEST ANSWER

It seems that you're trying to compile a C++ program, but there is no main function in your code, which is the entry point of a C++ program. Additionally, you're using functions like gluPerspective from the OpenGL utility library (GLU), but you haven't linked against it.

First Add a main function

int main() {
CGfxOpenGL renderer; // Create an instance of your renderer
renderer.Init(); // Initialize OpenGL
renderer.SetupProjection(width, height); // Set up projection

while (/* condition for your rendering loop */) {
    renderer.Prepare(dt); // Update any necessary data
    renderer.Render(); // Render the scene
    // Additional code for rendering loop logic
}

renderer.Shutdown(); // Clean up resources

return 0;
}

Link the OpenGL Utility Library (GLU):

g++ -o test learnopengl2triangleglew.cpp -lGL -lGLU -ldl

Set the values for the width and height

int main() {
    // ...
    int width = 800;
    int height = 600;
    // ...
}

Remember, you will need a loop for rendering and updating also you may need to handle user input. Additionally make sure that you have initialized an OpenGL context before using any OpenGL functions.