C++ OpenGL 运行时错误
C++ OpenGL Runtime Error
我目前正在学习 OpenGL 的 C++ tutorial,我想尝试编写自己的程序,它只会绘制四个向量。我遵循了教程模型代码,只是稍微修改了一下。这是我项目中的文件:
Main.cpp:
#include "Render.h"
#include <iostream>
void runMainLoop(int val);
int main(int argc, char* args[])
{
glutInit(&argc, args); //Starts OpenGL
glutInitDisplayMode(GLUT_DOUBLE); //Creates a double buffered display window
glutInitWindowSize(screenWidth, screenHeight); //Sets window size(screenWidth and screenHeight are set in Render.h)
glutCreateWindow("Display"); //Creates window
glutDisplayFunc(render);
glutTimerFunc(1000 / screenFPS, runMainLoop, 0);
glutMainLoop();
return 0;
}
void runMainLoop(int val)
{
render();
glutTimerFunc( 1000 / screenFPS, runMainLoop, val );
}
Render.cpp:
#include "Render.h"
void render()
{
glClear( GL_COLOR_BUFFER_BIT ); //Clears screen
glLoadIdentity();
glBegin( GL_QUADS ); //Vectors
glColor3f(1.f, 0.f, 0.f);
glVertex2f(-1.f, -1.f);
glVertex2f(1.f, -1.f);
glVertex2f(1.f, 1.f);
glVertex2f(-1.f, 1.f);
glEnd(); //End of vectors
glutSwapBuffers(); //Drawing (This swaps the already-drawn memory with the current display of vectors. This is so you don't get a live draw, which the user would be able to see.)
}
Render.h:
#ifndef RENDER_H
#define RENDER_H
#include "OpenGLlib.h"
int screenWidth = 500;
int screenHeight = 500;
int screenFPS = 60;
void render();
#endif
最后是我的 OpenGL 库,OpenGLlib.h:
#ifndef OPENGLLIB_H
#define OPENGLLIB_H
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
问题出在构建过程中,这是构建日志中出现的错误:
obj\Debug\Render.o:Render.cpp:(.data+0x0): multiple definition of `screenWidth'
obj\Debug\Main.o:Main.cpp:(.data+0x0): first defined here
obj\Debug\Render.o:Render.cpp:(.data+0x4): multiple definition of `screenHeight'
obj\Debug\Main.o:Main.cpp:(.data+0x4): first defined here
obj\Debug\Render.o:Render.cpp:(.data+0x8): multiple definition of `screenFPS'
obj\Debug\Main.o:Main.cpp:(.data+0x8): first defined here
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
我在我的代码中没有看到任何其他 sceenWidth、screenHeight 和 screenFPS 定义。编译器读取的文件是否多于我项目中的文件?顺便说一句,我正在使用Code::Blocks。
Render.h 包含在两个文件中,并且有三个未声明为外部的变量。这意味着每个翻译单元(*.o 文件)都会获得 header 中变量的全局实例。这不好,因为链接器不能很好地解决这个问题;每个翻译单元都会在其所有代码中使用它的本地副本,因此它无法摆脱一个。它也不能同时保留两者,因为它们有名称冲突。
你要的是把Render.h改成
#ifndef RENDER_H
#define RENDER_H
#include "OpenGLlib.h"
extern int screenWidth;
extern int screenHeight;
extern int screenFPS;
void render();
#endif
并在 main.c
#include "Render.h"
#include <iostream>
int screenWidth = 500;
int screenHeight = 500;
int screenFPS = 60;
void runMainLoop(int val);
int main(int argc, char* args[])
{
glutInit(&argc, args); //Starts OpenGL
glutInitDisplayMode(GLUT_DOUBLE); //Creates a double buffered display window
glutInitWindowSize(screenWidth, screenHeight); //Sets window size(screenWidth and screenHeight are set in Render.h)
glutCreateWindow("Display"); //Creates window
glutDisplayFunc(render);
glutTimerFunc(1000 / screenFPS, runMainLoop, 0);
glutMainLoop();
return 0;
}
void runMainLoop(int val)
{
render();
glutTimerFunc( 1000 / screenFPS, runMainLoop, val );
}
我目前正在学习 OpenGL 的 C++ tutorial,我想尝试编写自己的程序,它只会绘制四个向量。我遵循了教程模型代码,只是稍微修改了一下。这是我项目中的文件:
Main.cpp:
#include "Render.h"
#include <iostream>
void runMainLoop(int val);
int main(int argc, char* args[])
{
glutInit(&argc, args); //Starts OpenGL
glutInitDisplayMode(GLUT_DOUBLE); //Creates a double buffered display window
glutInitWindowSize(screenWidth, screenHeight); //Sets window size(screenWidth and screenHeight are set in Render.h)
glutCreateWindow("Display"); //Creates window
glutDisplayFunc(render);
glutTimerFunc(1000 / screenFPS, runMainLoop, 0);
glutMainLoop();
return 0;
}
void runMainLoop(int val)
{
render();
glutTimerFunc( 1000 / screenFPS, runMainLoop, val );
}
Render.cpp:
#include "Render.h"
void render()
{
glClear( GL_COLOR_BUFFER_BIT ); //Clears screen
glLoadIdentity();
glBegin( GL_QUADS ); //Vectors
glColor3f(1.f, 0.f, 0.f);
glVertex2f(-1.f, -1.f);
glVertex2f(1.f, -1.f);
glVertex2f(1.f, 1.f);
glVertex2f(-1.f, 1.f);
glEnd(); //End of vectors
glutSwapBuffers(); //Drawing (This swaps the already-drawn memory with the current display of vectors. This is so you don't get a live draw, which the user would be able to see.)
}
Render.h:
#ifndef RENDER_H
#define RENDER_H
#include "OpenGLlib.h"
int screenWidth = 500;
int screenHeight = 500;
int screenFPS = 60;
void render();
#endif
最后是我的 OpenGL 库,OpenGLlib.h:
#ifndef OPENGLLIB_H
#define OPENGLLIB_H
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
问题出在构建过程中,这是构建日志中出现的错误:
obj\Debug\Render.o:Render.cpp:(.data+0x0): multiple definition of `screenWidth'
obj\Debug\Main.o:Main.cpp:(.data+0x0): first defined here
obj\Debug\Render.o:Render.cpp:(.data+0x4): multiple definition of `screenHeight'
obj\Debug\Main.o:Main.cpp:(.data+0x4): first defined here
obj\Debug\Render.o:Render.cpp:(.data+0x8): multiple definition of `screenFPS'
obj\Debug\Main.o:Main.cpp:(.data+0x8): first defined here
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
我在我的代码中没有看到任何其他 sceenWidth、screenHeight 和 screenFPS 定义。编译器读取的文件是否多于我项目中的文件?顺便说一句,我正在使用Code::Blocks。
Render.h 包含在两个文件中,并且有三个未声明为外部的变量。这意味着每个翻译单元(*.o 文件)都会获得 header 中变量的全局实例。这不好,因为链接器不能很好地解决这个问题;每个翻译单元都会在其所有代码中使用它的本地副本,因此它无法摆脱一个。它也不能同时保留两者,因为它们有名称冲突。
你要的是把Render.h改成
#ifndef RENDER_H
#define RENDER_H
#include "OpenGLlib.h"
extern int screenWidth;
extern int screenHeight;
extern int screenFPS;
void render();
#endif
并在 main.c
#include "Render.h"
#include <iostream>
int screenWidth = 500;
int screenHeight = 500;
int screenFPS = 60;
void runMainLoop(int val);
int main(int argc, char* args[])
{
glutInit(&argc, args); //Starts OpenGL
glutInitDisplayMode(GLUT_DOUBLE); //Creates a double buffered display window
glutInitWindowSize(screenWidth, screenHeight); //Sets window size(screenWidth and screenHeight are set in Render.h)
glutCreateWindow("Display"); //Creates window
glutDisplayFunc(render);
glutTimerFunc(1000 / screenFPS, runMainLoop, 0);
glutMainLoop();
return 0;
}
void runMainLoop(int val)
{
render();
glutTimerFunc( 1000 / screenFPS, runMainLoop, val );
}