1

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:

  • Install GLEW from here.
  • Install GLFW from here.
  • Unzip GLEW & GLFW
  • Go into the GLEW folder
  • Run make, sudo make install and make clean (following this)
  • Go into the GLFW folder
  • Run mkdir build, cd build and cmake .. (following this)
    This is where it gets tricky.
  • Run mkdir test-project, mkdir test-project/dependencies, mkdir test-project/dependencies/include, mkdir test-project/dependencies/lib
  • Move GLFW folder in the GLFW include directory to the project includes directory
    I didn't know what I was supposed to link, so I skipped this part.
  • Move GL folder in the GLEW include directory to the project includes directory
  • Move the contents of the lib folder to the project lib directory
  • Create a main.cpp file in the project folder
  • Add this to main.cpp:
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main()
{
    return 0;
}
  • Go to the project folder and execute 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!

conaticus
  • 301
  • 1
  • 4
  • 14
  • Please share the details (ideally something approaching a [mre]) of at least one failed attempt including the error message(s). Focusing on a specific example should lead to better results than trying to explain things in general. –  Aug 11 '21 at 11:20
  • The compiler is looking for `GL/glu.h` under the usual system paths and `./dependencies/include` (as per the `-I` opt) and did not find one. Where did you copy it? –  Aug 11 '21 at 11:51
  • The structure is `test-project/dependencies/include/GL/[Header Files]`, which I believe should work. – conaticus Aug 11 '21 at 11:54
  • Is it there, is it readable? We can't check the contents of your file system. If you want to get a quick listing of a directory and everything below, running `find` without arguments is a practical way of doing that. (I suggest doing one on the whole project) –  Aug 11 '21 at 12:06
  • @dratenik No, there is no `glu.h` in the `GL` folder, or the entire project. Strangely this doesn't come with GLEW. – conaticus Aug 11 '21 at 12:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235883/discussion-between-samthefam-and-dratenik). – conaticus Aug 11 '21 at 12:16
  • My go-to suggestion for these kinds of issues is https://github.com/Polytonic/Glitter You can figure out CMake later. – Botje Aug 11 '21 at 14:02

1 Answers1

1

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

Muhammet Ali Asan
  • 1,486
  • 22
  • 39
  • 1
    Thank you. Do you mind editing this to show where I learn how to do this for other C++ libraries, I have searched for ages, and cannot find anything. – conaticus Aug 17 '21 at 08:20