OpenGL 项目返回未定义的引用
Posted
技术标签:
【中文标题】OpenGL 项目返回未定义的引用【英文标题】:OpenGL project returns with undefined references 【发布时间】:2012-10-10 02:45:13 【问题描述】:我正在关注来自 site 的 OpenGL 教程。我已经下载并安装(希望是正确的)使用的 OpenGL 库。 (GLEW,GLFW,GLM)。但是,当我从该站点编译代码时,我发现很多未定义引用的错误。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
using namespace glm;
int main( void )
// Initialize GLFW
if( !glfwInit() )
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE,GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
// Initialize GLEW
if (glewInit() != GLEW_OK)
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
glfwSetWindowTitle( "Playground" );
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
// Dark blue background
glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
do
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers();
// Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
错误:
"make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
"make" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/opengl_1.exe
make[2]: Entering directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
mkdir -p dist/Debug/MinGW-Windows
g++.exe -o dist/Debug/MinGW-Windows/opengl_1 build/Debug/MinGW-Windows/main.o
build/Debug/MinGW-Windows/main.o: In function `main':
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:21: undefined reference to `glfwInit'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:27: undefined reference to `glfwOpenWindowHint'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:28: undefined reference to `glfwOpenWindowHint'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:29: undefined reference to `glfwOpenWindowHint'
nbproject/Makefile-Debug.mk:62: recipe for target `dist/Debug/MinGW-Windows/opengl_1.exe' failed
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:30: undefined reference to `glfwOpenWindowHint'
make[2]: Leaving directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:31: undefined reference to `glfwOpenWindowHint'
nbproject/Makefile-Debug.mk:59: recipe for target `.build-conf' failed
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:34: undefined reference to `glfwOpenWindow'
make[1]: Leaving directory `/cygdrive/c/Users/jared/Documents/NetBeansProjects/opengl_1'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:37: undefined reference to `glfwTerminate'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:42: undefined reference to `_imp__glewInit@0'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:47: undefined reference to `glfwSetWindowTitle'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:50: undefined reference to `glfwEnable'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:53: undefined reference to `glClearColor@16'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:59: undefined reference to `glfwSwapBuffers'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:62: undefined reference to `glfwGetKey'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:63: undefined reference to `glfwGetWindowParam'
C:\Users\jared\Documents\NetBeansProjects\opengl_1/main.cpp:66: undefined reference to `glfwTerminate'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/opengl_1.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 875ms)
注意:我使用 Netbeans(带有 C++ 插件)作为 IDE。
【问题讨论】:
对 Netbeans 不太了解,但您似乎没有链接到 glfw 库。 如何链接到 glfw 库? ***.com/questions/2289569/… 可能会回答您的问题 【参考方案1】:重新阅读 makefile 输出,我发现您实际上并没有与 GL 库链接。您需要修改项目设置以包含库。
【讨论】:
他实际上没有链接到 GLFW(不是 OpenGL)。但同样的问题,只是不同的库。以上是关于OpenGL 项目返回未定义的引用的主要内容,如果未能解决你的问题,请参考以下文章
C ++错误:对“”的未定义引用,并且ld返回了1个退出状态[重复]
C++ 错误:对“<func name>”的未定义引用和 ld 返回 1 个退出状态 [重复]