如何在Mac上创建简单的OpenGL窗口?

时间:2017-10-27 09:48:22

标签: macos opengl

OSX版本:10.13 - High Sierra

Macbook Pro Late 2011

Xcode:9

等级:初学者

我查看了Apple doc中的OpenGL编程指南,但文档并不那么友好,所以我还没有尝试过。

我做了google research和youtube关于如何简单地创建一个入门窗口,我已经使用XCode成功创建了一个带有所需颜色矩形的窗口。但它是OpenGL v2,并且有许多弃用警告。

我想在XCode环境之外开发OpenGL,但我没能编译它,这里是我的main.cpp

#include <stdlib.h>
#include <OpenGL/gl.h>
#include <GLUT/glut.h>

void display () {

    glClearColor ( 1.f, 0.f, 0.f, 1.f );

    glClear ( GL_COLOR_BUFFER_BIT );

    glLoadIdentity ();

    glFlush ();

}

int main ( int argc, char** argv ) {

    glutInit ( &argc, argv );

    glutInitDisplayMode ( GLUT_SINGLE );

    glutInitWindowSize ( 640, 480 );

    glutInitWindowPosition ( 100, 100 );

    glutCreateWindow ( "Baby GL" );

    glutDisplayFunc ( display );

    glutMainLoop ();

    return 0;

}

我使用vscode + vim作为主文本编辑器

以下是我在终端中编译main.cpp的方法:

g++ -pipe --std=c++14 main.cpp -lm

我尝试在没有代码的情况下编译它们,并且库中包含没有抛出任何错误,之后我将所有代码复制粘贴回main,并编译它,它抛出了这个错误:

7 warnings generated. // just deprecation warning
Undefined symbols for architecture x86_64:
"_glClear", referenced from:
  display() in main-26b0d3.o
"_glClearColor", referenced from:
  display() in main-26b0d3.o
"_glFlush", referenced from:
  display() in main-26b0d3.o
"_glLoadIdentity", referenced from:
  display() in main-26b0d3.o
"_glutCreateWindow", referenced from:
  _main in main-26b0d3.o
"_glutDisplayFunc", referenced from:
  _main in main-26b0d3.o
"_glutInit", referenced from:
  _main in main-26b0d3.o
"_glutInitDisplayMode", referenced from:
  _main in main-26b0d3.o
"_glutInitWindowPosition", referenced from:
  _main in main-26b0d3.o
"_glutInitWindowSize", referenced from:
  _main in main-26b0d3.o
"_glutMainLoop", referenced from:
  _main in main-26b0d3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:0)

这些错误意味着链接器无法在任何地方找到这些函数。

您必须使用-framework Cocoa-framework OpenGL进行编译/链接。当您使用GLUT时,您还需要将其作为预编译框架下载并将-framework GLUT附加到编译/链接标记。