How to build GLFW3 project on Linux?
Asked Answered
M

3

6

I've compiled glfw3 and the included examples using cmake and make without problems. Onto writing my first project. Being new to opengl and glfw, and unexperienced with C and CMake, i'm struggling to understand the example build files, or even which libraries to link and/or compiler parameters to use in my project.

Let's say i have just one folder with one file, boing.c for now. How would i compile it?

Simply running gcc -lglfw3 -lm -lGL -lGLU boing.c gives a wall of undefined references, starting with sin and atan2, followed by various gl and glfw stuff. What am i missing?

How would i go about writing a makefile? Is there a cmake template or sample, that i just didn't understand how to use or adapt? Does anyone know about an open source project (or better, a small example or template) using glfw3 -- so i can look around?

I'm guessing cmake would be best, when i want to go multi-platform at some point. But how do i just get the **** thing to compile without too much hassle, so i can get started on some tutorials..?

I'm a moderate noob using 32bit Ubuntu raring. Just using Vim for now.

Mariehamn answered 9/7, 2013 at 22:18 Comment(3)
can you post the errors you are getting.Annam
@Annam Here are the errors: pastebin.com/0HjYNpkS I started learning Nimrod instead in the meantime. Awesome and elegant language.Mariehamn
Possible duplicate of How to build & install GLFW 3 and use it in a Linux projectQuantum
H
11

I would strongly suggest installing CMake if you haven't already.

Even though I suggest using CMake, it is understandably not the easiest tool to use, but is much better than making your own make files.

To compile boing.c (which btw, is an example provided by glwf for those who are unaware) Be in the glfw root directory and type cmake . then make

And that should build all of the examples.

But, to answer how to make a simple CMake file, here is an example CMakeLists.txt to build boing.c:

cmake_minimum_required(VERSION 2.8)

project( BOING_PROJECT ) # this is case sensitive 

######################################
# finds OpenGL, GLU and X11
find_package(OpenGL REQUIRED)
if(NOT OPENGL_FOUND)
    message("ERROR: OpenGL not found")
endif(NOT OPENGL_FOUND)
set(GL_LIBRARY GL GLU X11)

add_executable(boing boing.c)

# linking "glfw" and not "glfw3" 
# assumes that glfw was built with BUILD_SHARED_LIBS to ON
target_link_libraries(boing glfw ${GL_LIBRARY} m)

The directory structure of the above would be

boing_project/  
    boing.c  
    CMakeLists.txt  

However, that does not answer why your getting all those errors.

The cause of your errors is that you supplied the arguments to GCC in the wrong order, try
gcc boing.c -lglfw3 -lm -lGL -lGLU
or better
gcc boing.c -o boing -Wall -lglfw3 -lm -lGL -lGLU

To answer your question: are there any open source projects that use glfw that you can look through? Just search for glfw on github and you'll find plenty.

Haynes answered 30/7, 2013 at 4:17 Comment(2)
And if you, like me, are wondering why the order is important check this: https://mcmap.net/q/64297/-why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gccPoulenc
Provided code calls find_package(OpenGL) but does NOT use its results: GL and GLU are just libraries names which are searched by the linker by its own. Instead, ` ${OPENGL_LIBRARIES} should be used for link, as described in the FindOpenGL.cmake documentation. Or, better, use imported targets: OpenGL::GL and OpenGL::GLU.Jacobite
T
13

You need to include library like this

-lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi

#Ref : http://www.blogosfera.co.uk/2013/07/how-to-build-install-glfw-3-and-use-it-in-a-linux-project/

Sorry i'm not good in english

Tapioca answered 27/7, 2013 at 20:28 Comment(3)
Thanks, but no, it still gives me the exact same errors! Maybe there's a bug of some kind..Mariehamn
This answer is correct in the case that it gives the information that we need to link against additional libraries on linux, such as Xi and Xrandr. I'm not sure about the order though, I have GL after glfw3, do not know if that mathers. More details about this can be found here: #17768508Illampu
the order is glfw3 GL X11 pthread Xrandr Xi dl GLEW GLUBlackandwhite
H
11

I would strongly suggest installing CMake if you haven't already.

Even though I suggest using CMake, it is understandably not the easiest tool to use, but is much better than making your own make files.

To compile boing.c (which btw, is an example provided by glwf for those who are unaware) Be in the glfw root directory and type cmake . then make

And that should build all of the examples.

But, to answer how to make a simple CMake file, here is an example CMakeLists.txt to build boing.c:

cmake_minimum_required(VERSION 2.8)

project( BOING_PROJECT ) # this is case sensitive 

######################################
# finds OpenGL, GLU and X11
find_package(OpenGL REQUIRED)
if(NOT OPENGL_FOUND)
    message("ERROR: OpenGL not found")
endif(NOT OPENGL_FOUND)
set(GL_LIBRARY GL GLU X11)

add_executable(boing boing.c)

# linking "glfw" and not "glfw3" 
# assumes that glfw was built with BUILD_SHARED_LIBS to ON
target_link_libraries(boing glfw ${GL_LIBRARY} m)

The directory structure of the above would be

boing_project/  
    boing.c  
    CMakeLists.txt  

However, that does not answer why your getting all those errors.

The cause of your errors is that you supplied the arguments to GCC in the wrong order, try
gcc boing.c -lglfw3 -lm -lGL -lGLU
or better
gcc boing.c -o boing -Wall -lglfw3 -lm -lGL -lGLU

To answer your question: are there any open source projects that use glfw that you can look through? Just search for glfw on github and you'll find plenty.

Haynes answered 30/7, 2013 at 4:17 Comment(2)
And if you, like me, are wondering why the order is important check this: https://mcmap.net/q/64297/-why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gccPoulenc
Provided code calls find_package(OpenGL) but does NOT use its results: GL and GLU are just libraries names which are searched by the linker by its own. Instead, ` ${OPENGL_LIBRARIES} should be used for link, as described in the FindOpenGL.cmake documentation. Or, better, use imported targets: OpenGL::GL and OpenGL::GLU.Jacobite
A
0

I manage to build glfw projects using the following flags: Compiler flags:

-c -Wall

Linker flags:

-lglfw3 -lGLEW -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -ldl -lGL -lm -lpthread
Alpheus answered 4/2, 2016 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.