在Mac终端上编译OpenGL程序

Compile OpenGL program in Mac Terminal

本文关键字:OpenGL 程序 编译 Mac 终端      更新时间:2023-10-16

我使用的是完全更新了XCode 6.0.1版本的Mac Os X 10.9.5。我还安装了在安装XCode之后必须安装的命令行实用程序。我在我的openGL库中使用GLFW和GLEW。GLEW是手动安装的,而GLFW是用Macports安装的。

我想在没有XCode的情况下进行编码,因为我一直在Windows(使用MingW)和Linux上进行编码。我在谷歌上搜索了如何编译这个程序,还搜索了stackoverflow。

因此,当前我的编译命令如下(这是一个C++程序)

g++ -framework Cocoa -framework OpenGL -framework CoreFoundation -framework CoreVideo -framework IOKit -L/usr/lib -L/usr/local/lib -I/usr/include/GLFW -I/usr/local/include/GL main.cpp -o main

我不断得到以下错误:

Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
  _main in main-ba7a57.o
"_glfwInit", referenced from:
  _main in main-ba7a57.o
"_glfwMakeContextCurrent", referenced from:
  _main in main-ba7a57.o
"_glfwTerminate", referenced from:
  _main in main-ba7a57.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

程序是:

#define GLEW_STATIC
#include <glew.h>
#include <glfw3.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
GLFWwindow* window;
if(!glfwInit()) exit(EXIT_FAILURE);
window = glfwCreateWindow(1024, 768, "glfw", NULL, NULL);
if(!window) {
    glfwTerminate();
    exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
const GLubyte* version = glGetString(GL_VERSION);
cout << "OpenGL version: " << version << endl;
glfwTerminate();
return 0;
}

这只是猜测,但请尝试在命令行末尾添加"-lglfw"。这个命令告诉g++根据libglfw进行编译。

错误显示Undefined symbols for architecture x86_64这一事实让我认为您安装的库的版本可能是针对64位体系结构构建的。请记住,在选择要安装的库时,您应该根据要针对的体系结构而不是开发机器使用的体系结构来选择32位或64位版本。

请参阅其他问题以供参考。