Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 224
Mr. Robot Avatar asked Apr 19 '14 15:04

Mr. Robot


People also ask

Does GLFW work on Linux?

1.4 - What platforms are supported by GLFW? Currently, GLFW supports Windows (XP and later), macOS (10.8 Mountain Lion and later) and Unix-like operating systems with the X Window System, such as Linux, FreeBSD and Cygwin.

How do I compile a GLFW program?

To compile GLFW for Wayland, you need to have the Wayland and xkbcommon development packages installed. They are not needed to build or run programs that use GLFW. On Debian and derivates like Ubuntu and Linux Mint you will need the libwayland-dev , libxkbcommon-dev , wayland-protocols and extra-cmake-modules packages.

Does GLFW include OpenGL?

GLFW is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan development on the desktop. It provides a simple API for creating windows, contexts and surfaces, receiving input and events. GLFW is written in C and supports Windows, macOS, X11 and Wayland.


2 Answers

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

like image 119
lpapp Avatar answered Oct 11 '22 17:10

lpapp


I resolve it:

I added these parameters to linker:

-L/usr/local/lib -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11
like image 22
Mr. Robot Avatar answered Oct 11 '22 19:10

Mr. Robot