I apologize if this is a duplicate question, but I cannot find answers to this anywhere.
I am new to C++ and wish to start learning OpenGL. In order to do this, I need to setup both GLEW and GLFW. Despite reading documentation and doing a lot of research, I cannot figure out how to utilize C++ libraries statically and dynamically. The main reason I cannot find answers to this is due to the fact I am running on Ubuntu, whereas most resources are for doing so with Windows.
I have attempted to build the libraries with CMake, following documentation. I appear to successfully build the libraries, but the issue then comes when using these libraries with the compiler, which again I cannot find good enough answers to.
I have tried the following steps to installing GLEW and GLFW:
make
, sudo make install
and make clean
(following this)mkdir build
, cd build
and cmake ..
(following this)mkdir test-project
, mkdir test-project/dependencies
, mkdir test-project/dependencies/include
, mkdir test-project/dependencies/lib
GLFW
folder in the GLFW include directory to the project includes directoryGL
folder in the GLEW include directory to the project includes directorylib
folder to the project lib
directorymain.cpp
file in the project foldermain.cpp
:#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
{
return 0;
}
g++ ./main.cpp -I ./dependencies/include -L ./dependencies/lib
I then receive this error:In file included from ./main.cpp:1:
./dependencies/include/GL/glew.h:1205:14: fatal error: GL/glu.h: No such file or directory
1205 | # include <GL/glu.h>
| ^~~~~~~~~~
compilation terminated.
Please could somebody explain how C++ libraries work dynamically and statically, how you would install a library dynamically and statically, and provide some steps to using GLEW and GLFW on Ubuntu.
Any help would be greatly appreciated!
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