简体   繁体   English

无法在NetBeans中的Linux中使用C ++和OpenGL(GLFW)编译简单的源代码

[英]Can't compile easy source in C++ and OpenGL (GLFW) in Linux in NetBeans

I started to learn OpenGL (glfw) and I copy source from a tutorial and tried to compile it, but errors occured. 我开始学习OpenGL(glfw)并从教程中复制源代码并尝试编译它,但是发生了错误。 I think I have corectly installed all header files (glm, glfw etc.) 我想我已经corectly安装了所有头文件(glm,glfw等)

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;
 }

and this is the output in NetBeans: 这是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)   

Please help me. 请帮我。 Thank you for your time. 感谢您的时间。

First things first: 首先要做的事情:

this is my source (I didn't use this characters: <, > in header files.): 这是我的来源(我没有在头文件中使用这个字符:<,>。):

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. 你当前的include语句是错误的,我真的很惊讶它是如何以这种方式通过编译过程的。

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'

There might be the following options for the failure: 失败可能有以下选项:

  • You are not linking against the library (most likely) 你没有链接到库(最有可能)

  • You are not having the library installed (unlikely, based on your description) 您没有安装库(根据您的描述不太可能)

  • You are using symbols not present in the library (again, unlikely) 您正在使用库中不存在的符号(再次,不太可能)

The most probably reason is that you are not linking against the library, eventually. 最可能的原因是你最终没有链接到图书馆。 You should have this set up for the linker: 您应该为链接器设置此项:

-lglfw3

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

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. 由于您使用的是Netbeans IDE,因此除非您在后台手动编辑文件,否则您需要转到项目设置进行设置。 Here, you can see a screenshot which demonstrates that you have a linker tab where you can set up all this properly. 在这里,您可以看到一个屏幕截图,其中显示您有一个链接器选项卡,您可以在其中正确设置所有这些。

在此输入图像描述

I resolve it: 我解决了:

I added these parameters to linker: 我将这些参数添加到链接器:

-L/usr/local/lib -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM