0

I am very new to c++ and I am trying to use opengl with glfw. I am using ubuntu 18 on wsl. I have GL and GLFW on my include directory and I have included it in my code.

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
using namespace glm;

int main(){
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL 


    GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
    window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
    if( window == NULL ){
        fprintf( stderr, "Hi\n" );
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW
    glewExperimental=true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
}

I am using g++ to compile the program. When I use g++ -c main.cpp it compiles but throws a binary error when I try to run the program and when I use g++ main.cpp -IGL -IGLFW -Iglm I get that error. Am I missing something?

Error:

/usr/bin/ld: /tmp/cco9YYTw.o: in function `main':
main.cpp:(.text+0x17): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x26): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x35): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x44): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x53): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x74): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text+0xa4): undefined reference to `glfwTerminate'
/usr/bin/ld: main.cpp:(.text+0xb7): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: main.cpp:(.text+0xbd): undefined reference to `glewExperimental'
/usr/bin/ld: main.cpp:(.text+0xc3): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status
  • _"very new to c++ and I am trying to use opengl with glfw"_ uhm... shouldn't you start with something simple? Hello World or so? Please give the complete error message (in the question, not the comments) – JHBonarius Feb 14 '21 at 09:09
  • I am familiar with programming so I thought it would be a cool thing to do while I don't have anything else to do. Sorry about that I will post it asap. – Comely Cmly Feb 14 '21 at 09:14
  • @JHBonarius could you please take a look at it? If there isn't anything I can do, would you please suggest me something? I am planning to make a euler fluid simulation, something more beginner (c++) friendly. Thank you :) – Comely Cmly Feb 14 '21 at 09:18
  • Yeah, I'm looking at it ;) "I am planning to make a euler fluid simulation, something more beginner (c++) friendly." whahaha, beginner-friendly... most people don't even know what an euler fluid simulation is. xD – JHBonarius Feb 14 '21 at 09:22
  • 3
    @ComelyCmly Your errors are because the build command is not finding the libraries containing the missing references. Look to the `-l` option to name the libraries (make sure you get the spelling and case correct) and the `-L` option to tell g++ where to find the libraries. – john Feb 14 '21 at 09:22
  • @john Thank you for your suggestions. My spellings are correct or it seems like it . Could you please tell me how to do it properly? My GL and GLFW are located in the include directory (/usr/include) and the name is GL and GLFW. – Comely Cmly Feb 14 '21 at 09:25
  • Please take a look at the official [build guide](https://www.glfw.org/docs/latest/build_guide.html). – JHBonarius Feb 14 '21 at 09:29
  • @ComelyCmly If the **libraries** are in `/usr/include` then your installation is wrong. I think you are mixing up libraries with header files. They are not the same thing. – john Feb 14 '21 at 10:24

2 Answers2

2

To build your example you should install the following dependencies on your Ubuntu with apt-get

  • libglm-dev, libglfw-dev, libglew-dev, cmake

I've used CMake to build your file

Put the following CMakeLists.txt file in your folder where .cpp file located (I copied your code into a file named glfw_sample.cpp)

cmake_minimum_required(VERSION 3.10)

project(glfw_sample)

find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)

add_executable(${PROJECT_NAME} glfw_sample.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${GLFW_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE GLEW::GLEW ${GLFW_LIBRARIES})

Then execute in a shell

cmake -S. -Bbuild
cmake --build build

If no errors occurred on both steps you'll find glfw_sample executable in the build folder.

I use the following links to build your code:

  1. https://www.glfw.org/docs/3.0/build.html
  2. https://cmake.org/cmake/help/latest/module/FindGLEW.html
Sergei Nikulov
  • 5,029
  • 23
  • 36
0

A very quick google search might help: glfw-errors-with-glfwwindowhint

Link to -lglfw3 not -lglfw - if this is correct, please vote up the linked-to-answer and not this one

To cover the location of the library, from compile-glfw-on-ubuntu-and-fix-libglfw-so-cannot-open-error

This is because the GLFW installer puts libglfw.so in /usr/local/lib instead of /usr/lib like Ubuntu expects. But /usr/local/lib is a fine place to put shared libraries for most of the Unix world, so let’s just tell Ubuntu to look there when linking our program. You’ll want to edit /etc/ld.so.conf and add the line “/usr/local/lib” to the file, then save. Afterwards you should run ldconfig as root. Something like this:

sudo vim /etc/ld.so.conf
# Add the path and save - then try to build again
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • When I do this I get an error /usr/bin/ld: cannot find -lglfw3. I guess my g++ is looking in /bin/ld, how do I change it such that it looks in include folder? – Comely Cmly Feb 14 '21 at 09:32
  • Add a linker path `-L` – code_fodder Feb 14 '21 at 09:34
  • -L/usr/include/GL? sorry if I am being annoying – Comely Cmly Feb 14 '21 at 09:51
  • Don't forget to install the following dependencies on your Ubuntu with apt-get: libglm-dev, libglfw-dev, libglew-dev. – Sergei Nikulov Feb 14 '21 at 10:10
  • I have posted an update for you. But, no, libs are never in the `include` paths they are in `..../lib/` paths. You could quicly try `-L/usr/local/lib` but I would recommend you make sure that is where the `libglfw3.so` file is and then add it to the `/etc/ld.so.conf` file as described above – code_fodder Feb 14 '21 at 10:11