当我尝试使用以下命令用gcc编译用c语言编写的opengl代码时,它运行良好:
gcc -Wall tutorial10.c -lGL -lglut -lGLU
但是当我尝试使用g ++进行相同的编译时
g++ -Wall tutorial10.c -lGL -lglut -lGLU
它开始产生很多这样的错误:
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
评论标题:
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
您的翻译单元中没有opengl函数声明(很可能没有包含<GL/gl.h>
)。
gcc
之所以这样做,是因为较旧的C语言版本允许隐式声明-每当您使用函数时,gcc都会使用通用规则自动声明它。它不意味着功能会得到妥善称为-潜规则是非常通用的,将导致问题如float
参数类型,所以我们期待了很多错误的位置。
如果将cflag添加-Wimplicit-function-declaration
(或-Wall
包括在内的声明,以及许多其他声明),则会看到有关隐式声明的警告。
g++
另一方面,启用了C ++模式,该模式禁止隐式声明。
总而言之,除非您真正了解隐式声明,否则不要使用隐式声明(但是在那种情况下,您还是不想使用它们)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句