Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation issues in OpenGL code with g++ [duplicate]

Tags:

c++

g++

opengl

When I try compiling the opengl Code written in c language with gcc with the following commands it runs fine :

gcc -Wall tutorial10.c -lGL -lglut -lGLU

But when I try doing the same compiling it by using g++

g++ -Wall tutorial10.c -lGL -lglut -lGLU

It starts to give a lot of errors like this :

tutorial10.c: In function ‘void drawRect()’:
tutorial10.c:28:34: error: ‘glClearBufferfv’ was not declared in this scope
tutorial10.c:34:28: error: ‘glUseProgram’ was not declared in this scope
tutorial10.c:36:24: error: ‘glGenBuffers’ was not declared in this scope
tutorial10.c:37:37: error: ‘glBindBuffer’ was not declared in this scope
tutorial10.c:47:71: error: ‘glBufferData’ was not declared in this scope
tutorial10.c:49:52: error: ‘glVertexAttribPointer’ was not declared in this scope
tutorial10.c:50:29: error: ‘glEnableVertexAttribArray’ was not declared in this scope
tutorial10.c:52:60: error: ‘glGetUniformLocation’ was not declared in this scope
tutorial10.c:54:42: error: ‘glUniform4f’ was not declared in this scope
tutorial10.c:59:30: error: ‘glDisableVertexAttribArray’ was not declared in this scope
tutorial10.c: In function ‘int main(int, char**)’:
tutorial10.c:93:34: error: ‘glCreateProgram’ was not declared in this scope
tutorial10.c:95:54: error: ‘glCreateShader’ was not declared in this scope
tutorial10.c:124:76: error: ‘glShaderSource’ was not declared in this scope
tutorial10.c:128:36: error: ‘glCompileShader’ was not declared in this scope
tutorial10.c:134:49: error: ‘glAttachShader’ was not declared in this scope
tutorial10.c:136:29: error: ‘glLinkProgram’ was not declared in this scope
tutorial10.c:147:35: error: ‘glDeleteShader’ was not declared in this scope

Headers from comment:

#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
like image 375
user3640759 Avatar asked Sep 30 '22 18:09

user3640759


1 Answers

You don't have opengl functions declarations within your translation unit (most likely, you haven't included <GL/gl.h>).

gcc takes that because older versions of C language allows implicit declarations - whenever you use function, gcc automatically declares it with generic rules. It does NOT mean functions will be called properly - implicit rules are very generic and will cause problems on e.g. float argument types, so expect a lot of bugs here.

You can see warnings about implicit declarations if you add -Wimplicit-function-declaration (or -Wall, which includes this, along with many others) to your cflags.

g++, on other side, enabled C++ mode, which forbids implicit declarations.

To sum it all, don't use implicit declarations unless you truly understand what they do (but in that case you wouldn't want to use them anyway).

like image 117
keltar Avatar answered Oct 03 '22 06:10

keltar