我开始学习OpenGL(glfw)并从教程中复制源代码并尝试编译它,但是发生了错误.我想我已经corectly安装了所有头文件(glm,glfw等)
这是我的来源(我没有在头文件中使用这些字符:<,>):
#include iostream #include stdio.h #include stdlib.h #include GL/glew.h #include GLFW/glfw3.h #include glm/glm.hpp #define GLFW_INCLUDE_GL_3 using namespace glm; using namespace std; int main(){ if(!glfwInit()){ return -1; } GLFWwindow* window; // (In the accompanying source code,this variable is global) window = glfwCreateWindow( 1024,768,"Tutorial 01",NULL,NULL); if( window == NULL ) { 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; } glfwMakeContextCurrent(window); // Initialize GLEW glewExperimental=true; // Needed in core profile if (glewInit() != GLEW_OK) { fprintf(stderr,"Failed to initialize GLEW\n"); return -1; } return 0; }
这是NetBeans中的输出:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf make[1]: Entering directory `/home/jan/NetBeansProjects/a' "/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/a make[2]: Entering directory `/home/jan/NetBeansProjects/a' mkdir -p dist/Debug/GNU-Linux-x86 g++ -o dist/Debug/GNU-Linux-x86/a build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/main.o: In function `main': /home/jan/NetBeansProjects/a/main.cpp:12: undefined reference to `glfwInit' /home/jan/NetBeansProjects/a/main.cpp:16: undefined reference to `glfwCreateWindow' /home/jan/NetBeansProjects/a/main.cpp:19: undefined reference to `glfwTerminate' /home/jan/NetBeansProjects/a/main.cpp:22: undefined reference to `glfwMakeContextCurrent' /home/jan/NetBeansProjects/a/main.cpp:25: undefined reference to `glewExperimental' /home/jan/NetBeansProjects/a/main.cpp:26: undefined reference to `glewInit' collect2: error: ld returned 1 exit status make[2]: *** [dist/Debug/GNU-Linux-x86/a] Error 1 make[2]: Leaving directory `/home/jan/NetBeansProjects/a' make[1]: *** [.build-conf] Error 2 make[1]: Leaving directory `/home/jan/NetBeansProjects/a' make: *** [.build-impl] Error 2 BUILD Failed (exit value 2,total time: 462ms)
请帮我.感谢您的时间.
解决方法
首先要做的事情:
this is my source (I didn’t use this characters: <,> in header files.):
那是错的,你应该这样做.你当前的include语句是错误的,我真的很惊讶它是如何以这种方式通过编译过程的.
/home/jan/NetBeansProjects/a/main.cpp:12: undefined reference to `glfwInit' /home/jan/NetBeansProjects/a/main.cpp:16: undefined reference to `glfwCreateWindow' /home/jan/NetBeansProjects/a/main.cpp:19: undefined reference to `glfwTerminate' /home/jan/NetBeansProjects/a/main.cpp:22: undefined reference to `glfwMakeContextCurrent' /home/jan/NetBeansProjects/a/main.cpp:25: undefined reference to `glewExperimental' /home/jan/NetBeansProjects/a/main.cpp:26: undefined reference to `glewInit'
失败可能有以下选项:
>您没有链接到库(最有可能)
>您没有安装库(根据您的描述不太可能)
>您正在使用库中不存在的符号(再次,不太可能)
最可能的原因是你最终没有链接到图书馆.您应该为链接器设置此项:
-lglfw3
请注意,当您开始添加这些内容时,您还需要添加作为依赖项出现的链中的所有内容,因此根据您的注释,这是要添加的整个链:
-L/usr/local/lib -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11
由于您使用的是Netbeans IDE,因此除非您在后台手动编辑文件,否则需要转到项目设置进行设置.在这里,您可以看到一个屏幕截图,演示您有一个链接器选项卡,您可以在其中正确设置所有这些.