未找到架构x86_64的符号(OpenGL with xcode)
我对使用OpenGL很新颖。我试图运行的程序是由我的教授提供的,所以我实际上没有写任何程序,我在运行程序时遇到问题。该程序假设只是在黑色屏幕上制作一个白色正方形。我正在使用mac Sierra 10.12.2。此外,我已经将部署目标更改为10.8,因为在晚些时候编译的错误。现在,当我尝试构建并在xcode中运行时,我得到2个错误。未找到架构x86_64的符号(OpenGL with xcode)
这些都是错误的即时得到,
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
}
任何帮助将不胜感激!
您需要添加下面靠近你的源文件的顶部:
#include <stdlib.h>
并删除这一行:
void exit(int) ;
首先,你应该始终使用适当的系统头,以获得系统库函数的声明。这在macOS中尤其如此,其中声明可以具有影响函数如何链接的重要属性。
但是,在这种情况下,缺乏这样的属性并不是真正的问题。这里的问题是你正在构建一个C++程序。在C++中,函数的参数类型是其符号名称的一部分。你可以在你引用的错误信息中看到这一点。但是exit()
函数是C标准库的一部分。它本身不是C++接口。它的符号名称是_exit
,没有指示其参数数量或类型。
您的代码已合并对符号的引用,该符号转换为exit(int)
,而系统库中的实际符号仅为_exit
。他们不匹配,所以你会得到一个符号未找到的链接器错误。
当stdlib.h头文件包含在C++翻译单元中时,stdlib.h头文件要特别小心地将它的函数声明包装在extern "C" { ... }
中。因此,包含该头文件以获取该声明会告诉C++编译器不要使用C++风格的符号名称,而要使用C风格的符号名称。
您也可以通过在您自己的代码中声明exit()
将extern "C"
“解决”该问题,但这是错误的方法。只需包含正确的标题。
非常感谢,我改变了它,它效果很好!所以有些奇怪的是,在我知道这一点上,我们不应该包括正确的库。所以我刚开始添加库只是为了看看它会如何工作,并且#include
我很郁闷,教授们仍然在这样教OpenGL。你可能想和你的教授坐下来,提醒他们[他们不应该教Legacy OpenGL](http://gamedev.stackexchange.com/questions/34108/opengl-vbo-or-glbegin-glend),应该坚持到2008年还没有被弃用的OpenGL函数。我引用的帖子引用了大量的话:“*有些人认为先学习老东西比较好,因为它更容易一点,但是你学到的东西大多是无用的,必须忘掉。*“。 – Xirema
仅供参考,你得到的错误是链接错误的结果,特别是与你的教授使用'exit'有关。不知何故,在构建过程中的某个地方,您没有引用正确的目标文件来链接到最终的可执行文件。如果我对您的特定构建环境有所了解,我会发布更详细的诊断说明作为答案。 – Xirema
他想教2的理由。x是因为“它更适合教学,它更容易建立到4.x而不是学习4,然后学习更旧的东西”,这对我也没有任何意义。我可能是错的,但我认为退出功能是GLUT库的一部分,但我不确定。这就是我所能提供的所有帮助,就像我说过的,我今天才开始尝试使用它。谢谢您的帮助! – csm60