C++ 用g++;

C++ 用g++;,c++,opengl,g++,C++,Opengl,G++,当我尝试使用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 no

当我尝试使用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函数声明(很可能您没有包含

gcc
之所以这样做,是因为较旧版本的C语言允许隐式声明——无论何时使用函数,gcc都会使用通用规则自动声明它。这并不意味着函数将被正确调用-隐式规则非常通用,并且会导致诸如
float
参数类型的问题,因此这里可能会有很多错误

如果将
-Wimplicit函数声明
(或
-Wall
,其中包括此声明和许多其他声明)添加到CFLAG中,则可以看到有关隐式声明的警告

<代码> G++< /COD>,另一方面,启用C++模式,禁止隐式声明。< /P>


总而言之,除非您真正了解隐式声明的作用(但在这种情况下,您无论如何都不想使用它们)。

这意味着您没有包含正确的标题。GCC检查文件扩展名以了解您正在编译的语言。以
.c
结尾的文件将始终编译为c代码。无论您是否使用
g++
。如果这是一个标题问题,为什么gcc能够正确编译并运行它?看起来固定函数管道可以用g++编译并正常工作,但不能使用产生上述错误的可编程管道。您的Include看起来如何?它不工作。添加标题不会进行任何更改。我认为冲突与我在代码中使用的可编程管道方法有关。固定函数管道使用g++可以很好地编译。因为
gl.h
只有gl1.2的声明。对于较新的版本,您应该在运行时使用例如GLEW(或手动)获取扩展函数指针,或者直接链接到它们,风险自负。如果您想转到后面的路径,请查找例如
glext.h
gl2.h
。好的,我将尝试找出适合g++的标题。谢谢你的帮助。再一次,这不是g++的问题。如果在gcc版本中调用
glUniform4f
,它将被编译,但函数参数将被搞乱,因为隐式规则说它需要双倍而不是浮点。但是不管怎样,如果你的需求不是很独特,那么就使用GLEW,它是很棒的。创建上下文后,您只需要添加
glewInit()
,而
#include
@user3640759将是最简单的解决方案。它负责所有可能存在的GL函数声明。