LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-25-2024, 04:01 AM   #1
Al99
LQ Newbie
 
Registered: Nov 2020
Posts: 22

Rep: Reputation: Disabled
gl.h not linking


Hi I am doing the tutorial at https://lazyfoo.net/tutorials/OpenGL...ngl/index2.php

I have

main.cpp
https://drive.google.com/file/d/1tHB...usp=drive_link

LUtil.cpp
https://drive.google.com/file/d/10GN...usp=drive_link

LUtil.h
https://drive.google.com/file/d/1dtW...usp=drive_link

LOpenGL.h
https://drive.google.com/file/d/1Rkt...usp=drive_link

I am compiling with
g++ main.cpp LUtil.cpp -lGLU -lGL -lfreeglut

and I get

LUtil.cpp: In function ‘bool initGL()’:
LUtil.cpp:4:15: error: ‘GL_PROJECTION’ was not declared in this scope
4 | glMatrixMode( GL_PROJECTION );
| ^~~~~~~~~~~~~
LUtil.cpp:4:1: error: ‘glMatrixMode’ was not declared in this scope
4 | glMatrixMode( GL_PROJECTION );
| ^~~~~~~~~~~~
LUtil.cpp:5:1: error: ‘glLoadIdentity’ was not declared in this scope
5 | glLoadIdentity();
| ^~~~~~~~~~~~~~
LUtil.cpp:8:15: error: ‘GL_MODELVIEW’ was not declared in this scope
8 | glMatrixMode( GL_MODELVIEW );
| ^~~~~~~~~~~~
LUtil.cpp:10:1: warning: no return statement in function returning non-void [-Wreturn-type]
10 | }
| ^
LUtil.cpp: At global scope:
LUtil.cpp:13:13: error: expected constructor, destructor, or type conversion before ‘(’ token
13 | glClearColor( 0.f, 0.f, 0.f, 1.f );
| ^
LUtil.cpp:16:1: error: ‘GLenum’ does not name a type; did you mean ‘enum’?
16 | GLenum error = glGetError();
| ^~~~~~
| enum
LUtil.cpp:17:1: error: expected unqualified-id before ‘if’
17 | if( error != GL_NO_ERROR )
| ^~
LUtil.cpp:22:1: error: expected unqualified-id before ‘return’
22 | return true;
| ^~~~~~
LUtil.cpp:23:1: error: expected declaration before ‘}’ token
23 | }
| ^
LUtil.cpp: In function ‘void render()’:
LUtil.cpp:31:14: error: ‘GL_COLOR_BUFFER_BIT’ was not declared in this scope
31 | glClear( GL_COLOR_BUFFER_BIT );
| ^~~~~~~~~~~~~~~~~~~
LUtil.cpp:31:5: error: ‘glClear’ was not declared in this scope
31 | glClear( GL_COLOR_BUFFER_BIT );
| ^~~~~~~
LUtil.cpp: At global scope:
LUtil.cpp:35:8: error: expected constructor, destructor, or type conversion before ‘(’ token
35 | glBegin( GL_QUADS );
| ^
LUtil.cpp:36:15: error: expected constructor, destructor, or type conversion before ‘(’ token
36 | glVertex2f( -0.5f, -0.5f );
| ^
LUtil.cpp:37:15: error: expected constructor, destructor, or type conversion before ‘(’ token
37 | glVertex2f( 0.5f, -0.5f );
| ^
LUtil.cpp:38:15: error: expected constructor, destructor, or type conversion before ‘(’ token
38 | glVertex2f( 0.5f, 0.5f );
| ^
LUtil.cpp:39:15: error: expected constructor, destructor, or type conversion before ‘(’ token
39 | glVertex2f( -0.5f, 0.5f );
| ^
LUtil.cpp:40:8: error: expected constructor, destructor, or type conversion before ‘;’ token
40 | glEnd();
| ^
LUtil.cpp:43:18: error: expected constructor, destructor, or type conversion before ‘;’ token
43 | glutSwapBuffers();
| ^
LUtil.cpp: In function ‘void runMainLoop(int)’:
LUtil.cpp:52:27: error: ‘SCREEN_FPS’ was not declared in this scope
52 | glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );
| ^~~~~~~~~~
LUtil.cpp:52:5: error: ‘glutTimerFunc’ was not declared in this scope
52 | glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );

As far as I know these functions and types are defined in gl.h

why am I getting these errors?

thanks.
 
Old 01-25-2024, 06:22 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
if you are using one of the distros that split the development files into seperate packages ( debian etc ) you need to install them as well.
You also need to tell the compiler wher to find the header files, try:
Code:
keithhedger@Marvin [ / ]$ pkg-config --cflags gl --libs
-I/usr/include/libdrm -lGL
 
Old 01-25-2024, 12:57 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Putting that together into one block of code.

HelOpGl.cpp
Code:
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>

//Screen Constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_FPS = 60;

bool initGL();
void update();
void render();

bool initGL() {
    //Initialize Projection Matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    //Initialize Modelview Matrix
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    
    //Initialize clear color
    glClearColor( 0.f, 0.f, 0.f, 1.f );

    //Check for error
    GLenum error = glGetError();
    if( error != GL_NO_ERROR ) {
        printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
        return false;
    }

    return true;
}

void update(){
}

void render(){
    //Clear color buffer
    glClear( GL_COLOR_BUFFER_BIT );
    //Render quad
    glBegin( GL_QUADS );
        glVertex2f( -0.5f, -0.5f );
        glVertex2f(  0.5f, -0.5f );
        glVertex2f(  0.5f,  0.5f );
        glVertex2f( -0.5f,  0.5f );
    glEnd();
    //Update screen
    glutSwapBuffers();
}

void runMainLoop( int val );

int main( int argc, char* args[] )
{
    //Initialize FreeGLUT
    glutInit( &argc, args );
    
    //Create OpenGL 2.1 context
    glutInitContextVersion( 2, 1 );
    
    //Create Double Buffered Window
    glutInitDisplayMode( GLUT_DOUBLE );
    glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
    glutCreateWindow( "OpenGL" );
    
    //Do post window/context creation initialization
    if( !initGL() )
    {
        printf( "Unable to initialize graphics library!\n" );
        return 1;
    }
    
    //Set rendering function
    glutDisplayFunc( render );
    
    //Set main loop
    glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 );
    
    //Start GLUT main loop
    glutMainLoop();

    return 0;
}

void runMainLoop( int val ){
    //Frame logic
    update();
    render();

    //Run frame one more time
    glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );
}
I was able to compile that with
Code:
g++ HelOpGl.cpp -lm -lGL -lGLU -lglut -Wall -Werror -o HelOpGl
https://0x0.st/HGv4.png
 
Old 01-29-2024, 06:29 AM   #4
Al99
LQ Newbie
 
Registered: Nov 2020
Posts: 22

Original Poster
Rep: Reputation: Disabled
teckk: thanks for compiling this.
Why doesn't the compiler see gl h in the desperate files?

I want to stay as close to the tutorial as possible. Thanks.
 
Old 01-29-2024, 06:39 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Probably you need to specify the location
g++ -I <include dir> .....
 
Old 01-29-2024, 06:50 AM   #6
Al99
LQ Newbie
 
Registered: Nov 2020
Posts: 22

Original Poster
Rep: Reputation: Disabled
Do I do it all in one go when I compile?
 
Old 01-29-2024, 07:40 AM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Starting with LUtil.cpp LUtil.h main.cpp LOpenGL.h in a directory.

To compile that with:
Code:
g++ main.cpp LUtil.cpp -lm -lGL -lGLU -lglut -Wall -o Test
I had to add to the top of LUtil.cpp
Code:
#include "LOpenGL.h"
#include "LUtil.h"
The four code files listed on
https://lazyfoo.net/tutorials/OpenGL...ngl/index2.php
 
Old 02-01-2024, 04:59 AM   #8
Al99
LQ Newbie
 
Registered: Nov 2020
Posts: 22

Original Poster
Rep: Reputation: Disabled
Putting those includes in solved the problem. Thanks.
 
  


Reply

Tags
c++, opengl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem linking archive file (libz.a) - could not read symbols: file format not recognized severed_bit Linux - Embedded & Single-board computer 3 10-18-2017 01:09 AM
[SOLVED] issue linking .SO "g++: -lcyusb: linker input file unused because linking not done" rohaanembedded Red Hat 5 07-26-2013 01:14 AM
Static linking vs. dynamic linking? posix_memalign Programming 13 04-18-2010 02:27 PM
Problem in using both load time linking and runtime linking durgaprasad_j Linux - General 0 08-01-2006 03:49 AM
Pango and ATK not linking to correct Glib 70k51k Linux - Software 3 04-05-2004 12:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration