How to create and install GLFW 3 and use it in a Linux project

GLFW3

Last night I worked late trying to compile GLFW 3 Linux packages from source. This process took me a lot of time, only about 3 hours, partly because I am not familiar with CMake, and partly because I am not familiar with GLFW.

I hope this post saves you from the difficulties that I had yesterday! I thought I should do a short review and hopefully save you a few hours of your life ...

Thanks to "urraka", "b6" and "niklas" on the #glfw IRC channel, I managed to get a working version of glfw version 3.0.1.

It turns out that this is not a trivial process (of course, not for me, I am not an expert), since there is not much documentation on glfw3 on the Internet, especially about configuring with CMake.

I was asked to divide this into a question and answer section, and so I did it, and the parts of the answers are now lower.

Are you a GLFW employee or a member of the GLFW team?

If any of the GLFW3 developers sees this, then my message for them is to add the "configure GLFW3 on Windows, Mac OS X and Linux" to your site! It is very easy to write programs with GLFW, since the online documentation is not bad, quickly scan all available classes and modules, and you will be ready to go. The sample test project presented here is also very good. The two main problems that I discovered were, firstly, how do I configure GLFW3 on my system, and secondly, how do I build a GLFW3 project? These two things are probably not clear enough to the non-expert.

Edit

There was a brief overview today (Date: 2014-01-14), it looks like the GLFW website has undergone significant changes since the last time I looked, and now there is a section on collecting GLFW and GLFW release programs, which, I I think are new.

+78
linux build install cmake glfw
Jul 21 '13 at 0:30
source share
6 answers

Step 1: Install GLFW 3 on your system using CMAKE

For this installation, I used KUbuntu 13.04, 64bit.

The first step is to download the latest version (assuming future versions will work in a similar way) from www.glfw.org, possibly using this link .

The next step will be to extract the archive and open the terminal. cd into the glfw-3.XX directory and run cmake -G "Unix Makefiles" , you may need elevated privileges, and you may also need to install build dependencies first . To do this, try sudo apt-get build-dep glfw or sudo apt-get build-dep glfw3 or do it manually since I used sudo apt-get install cmake xorg-dev libglu1-mesa-dev ... be other libraries, which you need, for example, with the pthread libraries ... Apparently, I already had them. (See the -l options specified in the g ++ build phase below).

Now you can type make and then make install , which will probably require sudo first.

Well, you need to get detailed output in the last three stages of CMake, telling you what was built or where it was placed. (In /usr/include , for example.)

Step 2. Create a test program and compile

The next step is to run vim ("what ?! vim ?!" you say) or your preferred IDE / text editor ... I didn’t use vim, I used Kate, because I'm on KUbuntu 13.04 ... Anyway, download or copy the test program from here (at the bottom of the page) and save, exit.

Now compile with g++ -std=c++11 -c main.cpp - not sure if C ++ 11 is required , but I used nullptr , so I need it ... You might need to upgrade gcc to version 4.7, or the upcoming version 4.8 ... Information on this is here .

Then correct your mistakes if you typed the program manually or tried to be "too smart", but something didn’t work ... Then connect it using this monster! g++ main.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi So, you can see that in the section "Dependencies for installing the assembly" you can also check that you have GL, GLU , X11 Xxf86vm (whatever it is) Xrandr posix-thread and Xi (whatever it is) development libraries . Perhaps update your graphics drivers, I think GLFW 3 may require OpenGL version 3 or higher? Maybe someone can confirm this? You may also need to add linker options -ldl -lXinerama -lXcursor to make it work correctly if you get undefined dlclose (credit for @ user2255242).

And yes, I really need many -l s!

Step 3: You are done, had a good day!

I hope this information was correct and everything worked for you, and you enjoyed writing the GLFW test program. Also, I hope this guide has helped, or will help, several people in the future who have fought, as they did yesterday!

By the way, all tags are those things that I searched on stackoverflow, looking for an answer that was not there. (Until now.) I hope this is what you were looking for if you were in a similar position for me.

+112
Jul 21 '13 at 12:14
source share

Note that you do not need so much -l if you set glfw with the BUILD_SHARED_LIBS option. (You can enable this option by first running ccmake ).

Thus, sudo make install install the shared library in /usr/local/lib/libglfw.so . Then you can compile the sample file with a simple one:

 g++ main.cpp -L /usr/local/lib/ -lglfw 

Then, be sure to add / usr / local / lib / to the shared library search path before running your program. This can be done with:

 export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH} 

And you can put this in your ~/.bashrc so you don't have to type it all the time.

+17
Nov 23 '13 at 17:03
source share

I solved it this way

The pkg-config file describes all the necessary compile time and binding time flags and dependencies required to use the library.

 pkg-config --static --libs glfw3 

shows that

 -L/usr/local/lib -lglfw3 -lrt -lXrandr -lXinerama -lXi -lXcursor -lGL -lm -ldl -lXrender -ldrm -lXdamage -lX11-xcb -lxcb-glx -lxcb-dri2 -lxcb-dri3 -lxcb-present -lxcb-sync -lxshmfence -lXxf86vm -lXfixes -lXext -lX11 -lpthread -lxcb -lXau -lXdmcp 

I don’t know if all these libraries are needed for compilation, but for me it works ...

+15
Jul 03 '15 at 21:33
source share

Since the accepted answer does not allow more edits, I am going to generalize it with one copy-paste command (replace 3.2.1 with the latest version available on the first line):

 version="3.2.1" && \ wget "https://github.com/glfw/glfw/releases/download/${version}/glfw-${version}.zip" && \ unzip glfw-${version}.zip && \ cd glfw-${version} && \ sudo apt-get install cmake xorg-dev libglu1-mesa-dev && \ sudo cmake -G "Unix Makefiles" && \ sudo make && \ sudo make install 

If you want to compile the program, use the following commands:

 g++ -std=c++11 -c main.cpp && \ g++ main.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor 

If you are following the learnopengl.com tutorial, you may need to configure GLAD. In this case, click on this link.

http://glad.dav1d.de/#profile=core&specification=gl&api=gl%3D3.3&api=gles1%3Dnone&api=gles2%3Dnone&api=glsc2%3Dnone&language=c&loader=on

and then click the "Create" button in the lower right corner of the website and upload the ZIP file. Unzip it and compile the sources with the following command:

 g++ glad/src/glad.c -c -Iglad/include 

Now the commands to compile your program become:

 g++ -std=c++11 -c main.cpp -Iglad/include && \ g++ main.o glad.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor 
+7
Jun 18 '17 at 13:01 on
source share

Great guide, thanks. Given most of the instructions here, it is almost built for me, but I had one remaining error.

 /usr/bin/ld: //usr/local/lib/libglfw3.a(glx_context.co): undefined reference to symbol 'dlclose@@GLIBC_2.2.5' //lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status 

After searching for this error, I had to add -ldl to the command line.

 g++ main.cpp -lglfw3 -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -lpthread -ldl 

Then the hello GLFW sample application is compiled and linked.

I'm new to Linux, so I'm not quite sure what this additional library does exactly ... other than fixing my binding error. However, I see the cmd switch in the column above.

+2
Dec 29 '15 at 18:33
source share

If someone gets lazy and maybe doesn't know how to configure the shell for all these libraries and -ls, then I created a python script (you should have python3, most Linux users), which allows you to easily compile scripts and run them without worrying, it just has regular system calls, just neatly organized, I created it for myself, but maybe it will be useful: here it is

0
Jul 18 '18 at 10:11
source share



All Articles