在Mac终端上编译OpenGL程序

4
我正在使用完全更新的Mac Os X 10.9.5,XCode版本为6.0.1。在安装了XCode之后,我还安装了必须安装的命令行工具。我的OpenGL库中使用了GLFW和GLEW。其中,GLEW是手动安装的,而GLFW是通过Macports安装的。
我想不使用XCode进行编码,因为我之前在Windows(使用MingW)和Linux上编码时并没有使用XCode。我通过搜索谷歌和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;
}

1
你没有链接到GLFW库,只是添加了新的链接目录,但没有库文件。 - oblitum
GLFW库的位置可以通过使用-L/usr/local/lib来指定。所有库都可以通过-L参数来指定。 - rathorsunpreet
2个回答

0
只是猜测,但是尝试在命令行末尾添加“-lglfw”。这会告诉g++编译针对libglfw。

0
我认为错误信息“Undefined symbols for architecture x86_64”表明你安装的库可能是为了 64 位架构而构建的。在选择要安装的库时,请记住应该根据你打算针对的架构选择 32 位或 64 位版本,而不是你开发机器使用的架构。
参考this other question

好的,但是,然后可以告诉g++在编译时使用特定的架构。你怎么做呢?如果你说-arch i386或-arch x86_64,我已经使用过了,但仍然得到了相同的错误。 - rathorsunpreet
@rathorsunpreet 我的意思是问题可能出在你安装的库上,而不是你自己的代码上。重新安装GLFW和GLEW库,确保你安装了32位版本。 - Baher Ramzy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接