【发布时间】:2017-06-08 12:17:44
【问题描述】:
我对使用 OpenGL 很陌生。我正在尝试运行的程序是由我的教授提供的,所以我实际上并没有编写任何程序,我在让程序运行时遇到问题。该程序假设只是在黑屏上制作一个白色方块。我正在使用 mac Sierra 10.12.2。此外,我已经将部署目标更改为 10.8,因为在此之后编译时会出现错误。现在,当我尝试在 xcode 中构建和运行时,出现 2 个错误。
这些是我遇到的错误,
Undefined symbols for architecture x86_64:
"exit(int)", referenced from:
myKeyboard(unsigned char, int, int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
现在这是我试图编译的代码。
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
const int screenHeight = 480; // window height is 480
const int screenWidth = 640 ; //window width is 640
// <<<<<<<<<<<<<<<<<<<<< Prototypes >>>>>>>>>>>>>>>>>>
void exit(int) ;
// <<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
glPointSize(4.0); // a ?dot? is 4 by 4 pixels
glLineWidth(4.0); // a ?dot? is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
// <<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glBegin(GL_POINTS);
// glBegin(GL_LINE_STRIP) ;
// glBegin(GL_LINE_LOOP) ;
// glBegin(GL_POLYGON);
glVertex2i(289, 190); // Dubhe
glVertex2i(320, 128) ; // Merak
glVertex2i(239, 67) ; // Phecda
glVertex2i(194, 101) ; // Megrez
glVertex2i(129, 83) ; // Alioth
glVertex2i(75, 73) ; // Mizar
glVertex2i(74, 74) ; // Alcor
glEnd();
glFlush(); // send all output to display
}
// <<<<<<<<<<<<<<<<<<<<<<<< myKeyboard >>>>>>>>>>>>>>
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
switch(theKey)
{
case 'Q':
case 'q':
exit(-1); //terminate the program
default:
break; // do nothing
}
}
// <<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640, 480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Big Deep - Type Q or q to quit") ; // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutKeyboardFunc(myKeyboard); // register the keyboard action function
myInit();
glutMainLoop(); // go into a perpetual loop
}
任何帮助将不胜感激!
【问题讨论】:
-
我很郁闷教授们还在这样教OpenGL。你可能想和你的教授坐下来,提醒他们they should not be teaching Legacy OpenGL,并且应该坚持使用在 2008 年没有被弃用的 OpenGL 函数。我链接的帖子中引用的很好:“有些人认为学习旧东西第一个更好,因为它更容易一些,但是你学到的东西大多是无用的,然后是你必须忘掉的东西。“。
-
仅供参考,您遇到的错误是链接错误的结果,特别是与您的教授使用
exit有关。不知何故,在您的构建过程中,您没有引用正确的目标文件来链接到您的最终可执行文件。如果我对您的特定构建环境有所了解,我会发布更详细的诊断说明作为答案。 -
他想要教授 2.x 的原因是因为“它更适合教学,而且更容易建立 4.x,而不是学习 4 然后学习旧的东西”,这也没有对我来说也没有任何意义。我可能错了,但我认为 exit 函数是 GLUT 库的一部分,但老实说我不确定。这就是我真正能提供的所有帮助,就像我说我今天才开始尝试使用它一样。感谢您的帮助!