簡體   English   中英

無法在NetBeans中的Linux中使用C ++和OpenGL(GLFW)編譯簡單的源代碼

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

我開始學習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)   

請幫我。 感謝您的時間。

首先要做的事情:

這是我的來源(我沒有在頭文件中使用這個字符:<,>。):

那是錯的,你應該這樣做。 你當前的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,因此除非您在后台手動編輯文件,否則您需要轉到項目設置進行設置。 在這里,您可以看到一個屏幕截圖,其中顯示您有一個鏈接器選項卡,您可以在其中正確設置所有這些。

在此輸入圖像描述

我解決了:

我將這些參數添加到鏈接器:

-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