Building an OpenGL project in g++

In short, I'm trying to create an OpenGL application (a normal window with a white square), but even after installing OpenGL, nothing. How to add OpenGL and how to compile?

Added.

#include "GL/gl.h"  
#include "GL/glut.h"  

void display(){  
    glClear(GL_COLOR_BUFFER_BIT);  
    glBegin(GL_POINTS);  
    glColor3f(0.0, 0.0, 0.0);  
    glVertex2f(0.25, 0.25);  
    glEnd();    
    glFlush();  
}  

int main(int argc, char** argv){  
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
    glutInitWindowSize(240, 240);  
    glutInitWindowPosition(100, 740);  
    glutCreateWindow("TEST");  
    glClearColor(1.0, 1.0, 1.0, 1.0);  
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);  
    glutDisplayFunc(display);  
    glutMainLoop();  
}
/tmp/ccQZMEmz.o: In function `main':
openGL.cpp:(.text+0x65): undefined reference to `glutInit'
openGL.cpp:(.text+0x6f): undefined reference to `glutInitDisplayMode'
openGL.cpp:(.text+0x7e): undefined reference to `glutInitWindowSize'
openGL.cpp:(.text+0x8d): undefined reference to `glutInitWindowPosition'
openGL.cpp:(.text+0x97): undefined reference to `glutCreateWindow'
openGL.cpp:(.text+0x102): undefined reference to `glutDisplayFunc'
openGL.cpp:(.text+0x107): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status
Author: Qwertiy, 2011-08-07

4 answers

Maybe first you need to read the docks, how to create, and then try to create something?
The GL command definitions are located in the gl.h file, to enable which you need to write

#include <gl/gl.h>

To work with the GLU library, you need to include the glu.h file in the same way.
Unlike standard libraries, the GLUT package must be installed and connected separately.
Judging by the Ubuntu tag, you have the corresponding distribution, in which the compilation is performed by the commands:

qmake -project
qmake Ваш_проект.pro
make

When it is necessary located in the project directory.

UPD: Here is a decent (step-by-step) article Compiling in Ubuntu.
Write down exactly what you can't do.

 1
Author: Tanya, 2011-08-11 22:34:01

This command exactly compiles everything

g++ openGL.cpp -lglut
 1
Author: KoVadim, 2011-08-12 08:14:12

What exactly is not working? try this

g++ source.cpp -lGL

UPD.

In general, the problem, as expected, is in the linking. By default, the compiler looks for functions in the standard library, but does not find those that are not there, so it needs to explicitly specify in which library it should look for all this. All this is specified using the key -l and the library name immediately following the key. For example, you have a library somewhere named libsome_library. so, so here is the name libraries are highlighted in bold here.

Also, by default, the search for libraries takes place in the path specified in the PATH environment variable, but if your library is located on some "unusual" path, then this path either needs to be added to PATH

Export PATH=/some/path/to/library:$PATH

Where the path to the library is highlighted in bold, or again specify the compiler explicitly using the key -L

-L/some/path/to/library/

The result should be something like

g++ main.cpp -L/opt/opengl -lGL

So that see where your libraries are, what they are called, and go ahead; -)

 1
Author: AlexDenisov, 2011-08-12 11:25:24
$ g++ stackover.cc -o stackover -lGL -lglut -lGLU
$ ls
first3.cc  first.cc  ps-groupgl.cc  stackover  stackover.cc
$ ./stackover
$

It's simple.

 0
Author: Nik Stasov, 2020-05-22 13:24:05