Can't compile easy source in C++ and OpenGL (GLFW) in Linux in NetBeans
Asked Answered
D

2

8

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

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)   

Please help me. Thank you for your time.

Dedededen answered 19/4, 2014 at 15:28 Comment(4)
You are not linking against it?Asyut
Notice the "undefined reference" items listed above. Those need to be resolved.Acyl
@πάντα ῥεῖ: I do not see any netbeans solution in that post, however...Asyut
Yes, of course. It has to be fixed in the project settings of Netbeans, ideally.Asyut
F
12

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.

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. Here, you can see a screenshot which demonstrates that you have a linker tab where you can set up all this properly.

enter image description here

Ferrule answered 19/4, 2014 at 15:33 Comment(4)
I added the parameters to linker but errors occurred again but less than before. docs.google.com/file/d/0B_1o37MpTnYUTUY0SXhjSDQ4SE0/editDedededen
Do you know the equivalent process on Code::Blocks?Bowls
@LaszloPapp Yeah that was what I had to do, thanks. :)Bowls
This answer helped to resolve my issues with GLFW , X11 , and pthread: https://mcmap.net/q/1326150/-cmake-glfw-x11-included-but-39-xconvertselection-39-not-foundNatal
D
1

I resolve it:

I added these parameters to linker:

-L/usr/local/lib -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11
Dedededen answered 19/4, 2014 at 17:33 Comment(1)
There is no need to post an answer again, when there is one already. Also, please select an answer as per meta.stackexchange.com/questions/5234/…Asyut

© 2022 - 2024 — McMap. All rights reserved.