How to Build & Install Glfw 3 and Use It in a Linux Project

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

2020 Updated Answer

It is 2020 (7 years later) and I have learned more about Linux during this time. Specifically that it might not be a good idea to run sudo make install when installing libraries, as these may interfere with the package management system. (In this case apt as I am using Debian 10.)

If this is not correct, please correct me in the comments.

Alternative proposed solution

This information is taken from the GLFW docs, however I have expanded/streamlined the information which is relevant to Linux users.

  • Go to home directory and clone glfw repository from github
cd ~
git clone https://github.com/glfw/glfw.git
cd glfw
  • You can at this point create a build directory and follow the instructions here (glfw build instructions), however I chose not to do that. The following command still seems to work in 2020 however it is not explicitly stated by the glfw online instructions.
cmake -G "Unix Makefiles"
  • You may need to run sudo apt-get build-dep glfw3 before (?). I ran both this command and sudo apt install xorg-dev as per the instructions.

  • Finally run make

  • Now in your project directory, do the following. (Go to your project which uses the glfw libs)

  • Create a CMakeLists.txt, mine looks like this

CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
PROJECT(project)

SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_BUILD_TYPE DEBUG)

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

add_subdirectory(/home/<user>/glfw /home/<user>/glfw/src)


FIND_PACKAGE(OpenGL REQUIRED)

SET(SOURCE_FILES main.cpp)

ADD_EXECUTABLE(project ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(project glfw)
TARGET_LINK_LIBRARIES(project OpenGL::GL)
  • If you don't like CMake then I appologize but in my opinion it is the easiest way to get your project working quickly. I would recommend learning to use it, at least to a basic level. Regretably I do not know of any good CMake tutorial

  • Then do cmake . and make, your project should be built and linked against glfw3 shared lib

  • There is some way of creating a dynamic linked lib. I believe I have used the static method here. Please comment / add a section in this answer below if you know more than I do

  • This should work on other systems, if not let me know and I will help if I am able to

How to build GLFW3 project on Linux?

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.

Using C++ libraries on Linux dynamically and statically

Since you are using Ubuntu, you can use apt to install those libraries.

  1. sudo apt install libglfw3-dev

  2. sudo apt install libglew-dev

  3. sudo apt install libgl1-mesa-dev

After these you should have GLEW and GLFW installed on your system. In order to build I would use CMake. Simply have this folder structure :

├── build
├── CMakeLists.txt
└── main.cpp

where CMakeLists.txt is:

cmake_minimum_required(VERSION 3.17)

project(test LANGUAGES CXX)

add_executable(test main.cpp)
target_link_libraries(test glfw GLEW /usr/lib/x86_64-linux-gnu/libGL.so.1)

and main.cpp is:

#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main()
{
if (!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
return 1;
}

GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
if (!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
const GLubyte* version = glGetString(GL_VERSION); // version as a string
printf("Renderer: %s\n", renderer);
printf("OpenGL version supported %s\n", version);

glEnable(GL_DEPTH_TEST); // enable depth-testing
glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"

glfwTerminate();
return 0;
}

In order to build this app

  1. cd build
  2. cmake ..
  3. make
  4. ./test

P.S: example code is taken from here : https://antongerdelan.net/opengl/hellotriangle.html

Update:
CMake searches for default paths for libraries. In Unix systems, they are /usr/local/lib, /usr/lib/x86_64-linux-gnu/, /usr/lib etc. Same for include directories : /usr/include, /usr/include/x86_64-linux-gnu/ and /usr/local/include these are the paths used by libraries installed through package manager.
In case you want to include libraries from different paths. Let's do simple example:

I wrote a library and published it as :

.
├── CMakeLists.txt
├── untitled.cpp
└── untitled.h

where untitled.h:

#pragma once

class MyLibrary
{
public:
MyLibrary();
void doWork();
};

and untitled.cpp:

#include <untitled.h>
#include<iostream>

MyLibrary::MyLibrary()
{

}

void MyLibrary::doWork()
{
std::cout<<"do work is called"<<std::endl;
}

and my CMakeLists.txt :

cmake_minimum_required(VERSION 3.14)

project(untitled LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

add_library(untitled SHARED
untitled.h
untitled.cpp
) # I am generating shared library from these source files.

# Then I am telling cmake to generate Makefile so that when user does make install
# my library will be installed to folder /usr/local/untitled/
set_target_properties(untitled PROPERTIES PUBLIC_HEADER "untitled.h")
INSTALL(TARGETS untitled
LIBRARY DESTINATION untitled/lib
PUBLIC_HEADER DESTINATION untitled/include)

Now as a user of my library you downloaded source code and want to build for your system. You follow

mkdir build && cd build && cmake .. && make

then you run sudo make install . The output is like :

Install the project...
-- Install configuration: ""
-- Installing: /usr/local/untitled/lib/libuntitled.so
-- Installing: /usr/local/untitled/include/untitled.h

Then you want to use this library in your project.

cmake_minimum_required(VERSION 3.17)

project(test LANGUAGES CXX)

add_executable(test main.cpp)





# You should add these lines to your CMake file because now my library lives in an unusual path
target_include_directories(test PUBLIC /usr/local/untitled/include)
target_link_directories(test PUBLIC /usr/local/untitled/lib)

# And I have updated link_libraries section with X because there is an libX.so file
# under any of these library paths: /usr/local/lib, /usr/lib (default paths) and custom
# added library path : (usr/local/untitled/lib). In this case X is untitled.
target_link_libraries(test glfw GLEW /usr/lib/x86_64-linux-gnu/libGL.so.1 untitled)

and I am using library :

#include <untitled.h>

int main()
{
MyLibrary ml;
ml.doWork();

return 0;
}

To see where are your include directories in your system refer to this answer
and to see where are your libraries in your system refer to this answer



Related Topics



Leave a reply



Submit