How to build GLFW3 project on Linux?

c, cmake, glfw

Solution

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.

Problem

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.

Original source

Related problems