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 andsudo 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 .
andmake
, your project should be built and linked against glfw3 shared libThere 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, trygcc boing.c -lglfw3 -lm -lGL -lGLU
or bettergcc 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.
sudo apt install libglfw3-dev
sudo apt install libglew-dev
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
cd build
cmake ..
make
./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
Rename All Files in a Folder With a Prefix in a Single Command
How to Remove Cached Credentials from Git
How to Use Sed to Extract Substring
How to Fix Java.Lang.Module.Findexception: Module Java.Se.Ee Not Found
How to Send a HTML Email With the Bash Command "Sendmail"
How to Split One Text File into Multiple *.Txt Files
Finding Number Is Even/Odd in Assembly
Python Code to Check If Service Is Running or Not.
How to Tar a Directory Without Retaining the Directory Structure
How to Find Which Position a Word Is in a String
How to Reload Google Chrome Tab from Terminal
How to Get the Bssid of Currently Connected Network Through Bash
Uninstall Node.Js Using Linux Command Line
Mount Smb/Cifs Share Within a Docker Container
How to Display Number to Two Decimal Places in Bash Function
How to Use Variables in Bash Sed Command, Specific Example