【发布时间】:2012-08-26 18:31:55
【问题描述】:
这是我要运行的程序
////////////////////////////////////////////////////
// square.cpp
//
// Stripped down OpenGL program that draws a square.
//
// Sumanta Guha.
////////////////////////////////////////////////////
#include <iostream>
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
using namespace std;
// Drawing (display) routine.
void drawScene(void)
{
// Clear screen to background color.
glClear(GL_COLOR_BUFFER_BIT);
// Set foreground (or drawing) color.
glColor3f(0.0, 0.0, 0.0);
// Draw a polygon with specified vertices.
glBegin(GL_POLYGON);
glVertex3f(20.0, 20.0, 0.0);
glVertex3f(80.0, 20.0, 0.0);
glVertex3f(80.0, 80.0, 0.0);
glVertex3f(20.0, 80.0, 0.0);
glEnd();
// Flush created objects to the screen, i.e., force rendering.
glFlush();
}
// Initialization routine.
void setup(void)
{
// Set background (or clearing) color.
glClearColor(1.0, 1.0, 1.0, 0.0);
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
// Set viewport size to be entire OpenGL window.
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
// Set matrix mode to projection.
glMatrixMode(GL_PROJECTION);
// Clear current projection matrix to identity.
glLoadIdentity();
// Specify the orthographic (or perpendicular) projection,
// i.e., define the viewing box.
glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
// Set matrix mode to modelview.
glMatrixMode(GL_MODELVIEW);
// Clear current modelview matrix to identity.
glLoadIdentity();
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
// Press escape to exit.
case 27:
exit(0);
break;
default:
break;
}
}
// Main routine: defines window properties, creates window,
// registers callback routines and begins processing.
int main(int argc, char **argv)
{
// Initialize GLUT.
glutInit(&argc, argv);
// Set display mode as single-buffered and RGB color.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Set OpenGL window size.
glutInitWindowSize(500, 500);
// Set position of OpenGL window upper-left corner.
glutInitWindowPosition(100, 100);
// Create OpenGL window with title.
glutCreateWindow("square.cpp");
// Initialize.
setup();
// Register display routine.
glutDisplayFunc(drawScene);
// Register reshape routine.
glutReshapeFunc(resize);
// Register keyboard routine.
glutKeyboardFunc(keyInput);
// Begin processing.
glutMainLoop();
return 0;
}
我确定我安装了 glut 和 OpenGL 并且是最新的,这就是我遇到的错误(我正在按照 2011 年的书中列出的方式编译代码):
ubuntu:~/Downloads/Code$ gcc square.cpp -o square -I /usr/include/ -L /usr/lib -lglut -lGL -lGLU -lX11 /tmp/ccAq6h4h.o:square.cpp:函数 __static_initialization_and_destruction_0(int, int): 错误: 未定义引用 'std::ios_base::Init::Init()' /tmp/ccAq6h4h.o:square.cpp:函数 __static_initialization_and_destruction_0(int, int): error: undefined reference to 'std::ios_base::Init::~Init()' collect2: ld 返回 1 退出状态
我写了自己的代码,并把它作为一个 c 文件并编译了
【问题讨论】:
-
好的,当我使用 g++ 而不是 gcc 编译时,代码可以工作,作者是否有理由说使用 gcc 而不是 g++ 编译?我对此很陌生,我知道 gcc 编译为 c 程序,但使用 g++ 可以正确编译和链接?
-
如果作者告诉你使用 gcc,他们就是白痴。 C++ 不是 C,它甚至不是它的超集。如果您的代码是 C++,则需要 C++ 编译器。哪个是 g++。
-
您知道您正在使用已弃用的旧版 OpenGL,对吗?您可能发现了一个过时的教程。请参阅opengl.org/wiki/Legacy_OpenGL 了解更多信息。