0

I am trying to run a single program using OpenGL:

#include "glad/glad.h"
#include "GLFW/glfw3.h"

int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    return 0;
}

But if I try to compile it with this Makefile, I get the following errors:

g++ -Wall -Iinclude -lglut -lGL -lGLU -lGLEW -lm -L/usr/local/lib -o opengl src/main.cpp
/tmp/ccc3n0wR.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `glfwInit'
main.cpp:(.text+0x14): undefined reference to `glfwWindowHint'
main.cpp:(.text+0x23): undefined reference to `glfwWindowHint'
main.cpp:(.text+0x32): undefined reference to `glfwWindowHint'
collect2: error: ld returned 1 exit status
Makefile:10: recipe for target 'opengl' failed
make: *** [opengl] Error 1

I'm also using GLAD.

Result of glxinfo | grep version:

server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
OpenGL core profile version string: 4.6.0 NVIDIA 440.100
OpenGL core profile shading language version string: 4.60 NVIDIA
OpenGL version string: 4.6.0 NVIDIA 440.100
OpenGL shading language version string: 4.60 NVIDIA
OpenGL ES profile version string: OpenGL ES 3.2 NVIDIA 440.100
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
    GL_EXT_shader_group_vote, GL_EXT_shader_implicit_conversions,

Additional information: I have compiled glfw3 my self, and I am trying to follow this tutorial to learn OpenGL basics. Instead of using Microsoft Visual Studio 2019, I am using Visual Studio Code.

All the code I'm using can be found in this repository.

Antonio Gamiz Delgado
  • 1,871
  • 1
  • 12
  • 33
  • This answer is probably the right one: https://stackoverflow.com/a/24675715/5769463 Put your libraries in the right order (and after the object file!) – ead Aug 04 '20 at 11:07
  • I have read that question but I do not see the problem in my makefile. Supposedly, dependencies are in the correct order. – Antonio Gamiz Delgado Aug 04 '20 at 11:21
  • your object file (once main.cpp is compiled) depends on libraries which should come after the object file/main.cpp. – ead Aug 04 '20 at 11:25
  • If I use ` $(CC) $(CFLAGS) -o $@ src/main.cpp $(LIBS) $(INCLUDE)` I get the same error. – Antonio Gamiz Delgado Aug 04 '20 at 11:32
  • What is important is the command line with which it is built and not the Makefile. However, you are using g++ to build your program (on linux/windows?) but `glfw3` (what is it `glfw3.lib`, `glfw3.a`, `glfw3.so`?) is built by you with VisualCompiler? There are just too many things that are unclear and cannot be reproduced, so I'm afraid I will not be able to help. – ead Aug 04 '20 at 11:42
  • I'm using `Ubuntu 18.04` (in title). I do not know if it's `glfw3.lib`, `.a` or `.so`. I just compiled the glfw library using the instructions described in its repo. After that, I copied the content of `include` folder into my own include folder. Anything else I should add? Sorry for the problems, I'm quite inexperienced. – Antonio Gamiz Delgado Aug 04 '20 at 11:45
  • Are you linking against glfw3? In the command line, there is GLU, GLUT, GL, GLEW but not glfw3. [This answer](https://stackoverflow.com/a/12574400/2579738) is the right one for you: Add `-lglfw3` and `-L`. – BDL Aug 04 '20 at 11:47
  • But how can I found that folder? – Antonio Gamiz Delgado Aug 04 '20 at 11:53
  • Ok, after putting -glfw at the beginning it compiles: LIBS = -lglfw -lglut -lGL -lGLU -lGLEW -lm. Nonetheless, now I get: main.cpp:(.text+0xb1): undefined reference to `gladLoadGLLoader' – Antonio Gamiz Delgado Aug 04 '20 at 12:00
  • Oh, I had to add -ldl and src/glad.c too!!! Now everything is working. Thanks a lot. Maybe post a short answer to mark it as answered? – Antonio Gamiz Delgado Aug 04 '20 at 12:04

0 Answers0