问题描述
我开始学习OpenGL(glfw),我从一个教程复制源代码,并试图编译它,但错误发生。我想我已经corectly安装了所有的头文件(glm,glfw等)
I started to learn OpenGL (glfw) and I copy source from a tutorial and tried to compile it, but errors occured. I think I have corectly installed all header files (glm, glfw etc.)
这是我的源代码(我没有使用这些字符:<文件):
This is my source (I didn't use these characters: <, > in header files):
#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中的输出:
and this is the output in 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)
$ b b
请帮我。
Please help me. Thank you for your time.
推荐答案
第一件事:
这是错误的,你应该。
That is wrong, and you should. Your current include statements are wrong, and I am actually surprised how it passed the compilation process this way.
您在这里看到链接器错误:
You are seeing linker errors in here:
/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'
可能有以下选项失败:
-
您未与图书馆建立关联(很可能)
You are not linking against the library (most likely)
您没有安装库(不太可能根据您的说明)
You are not having the library installed (unlikely, based on your description)
再次,不太可能)
最可能的原因是您最终没有链接到库。您应该为链接器设置此设置:
The most probably reason is that you are not linking against the library, eventually. You should have this set up for the linker:
-lglfw3
请注意,当你开始添加这些链接时,你还需要添加链接中的所有内容,所以基于你的评论,这是整个链add:
Note that you will also need to add everything in the chain that comes up as a dependency when you start adding these, so based on your comment this is the whole chain to add:
-L/usr/local/lib -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11
由于您使用的是Netbeans IDE,因此您需要转到项目设置以进行设置,除非您手动编辑后台文件。在这里,您可以看到一个屏幕截图,显示您有一个链接器标签,您可以在其中正确设置所有这些。
Since you are using the Netbeans IDE, you will need to go to the project settings to set it up unless you edit the files in the background manually. Here, you can see a screenshot which demonstrates that you have a linker tab where you can set up all this properly.
这篇关于在NetBeans中的Linux中无法使用C ++和OpenGL(GLFW)编译容易的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!