0

I am using glfw and glew to create the window and it's giving me linker errors. How can I fix it. This is my first time installing glfw and glew so I could've missed a step in the installation. Any help?

I compile my code using just:

gcc main.c

I know there are some linker flags, but glfw and glew are not a part of them.

I use linux with the gcc compiler.

This is my code:

#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <stdbool.h>

int main(){
   glewExperimental = true;
   if(!glfwInit()){
    printf("failed to initialize GLFW\n");
    return -1;
   }
   glfwWindowHint(GLFW_SAMPLES, 4);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

   GLFWwindow* window;
   window = glfwCreateWindow(1024, 768, "Game", NULL, NULL);
   if(window == NULL){
    printf("Sorry to say this, but your OpenGL version must be 3.3 or above! Thanks for playing, but to continue you must update your video card drivers or if you use an old GPU you may have to replace it with a new one to play this game. I will be developing my game for OpenGL 1 and 2 soon so stay on touch.");
    glfwTerminate();
    return -1;
   }
   glfwMakeContentCurrent(window);
   glewExperimental = true;
   if(glewInit() != GLEW_OK){
    printf("Failed to initilize GLEW");
    return -1;
   }

}

This is the error log:

/usr/bin/ld: /tmp/cc74wuFz.o: in function `main':
main.c:(.text+0xe): undefined reference to `glewExperimental'
/usr/bin/ld: main.c:(.text+0x14): undefined reference to `glfwInit'
/usr/bin/ld: main.c:(.text+0x3d): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x4c): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x5b): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x6a): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x79): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x9a): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.c:(.text+0xbb): undefined reference to `glfwTerminate'
/usr/bin/ld: main.c:(.text+0xd3): undefined reference to `glfwMakeContentCurrent'
/usr/bin/ld: main.c:(.text+0xd9): undefined reference to `glewExperimental'
/usr/bin/ld: main.c:(.text+0xdf): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
RadoslavL
  • 19
  • 8
  • Quite obviously the linker can't find the OpenGL lib. So rather than posting your code, you should describe the steps you took when installing it. – Lundin Nov 11 '21 at 09:37
  • I just put the header files in /usr/local/include/GL and that's what i did. Please tell me if i need to export the linker files – RadoslavL Nov 11 '21 at 10:42
  • I use Linux (Linux Mint if thats importmant) with the gcc compiler. I don't have an IDE. – RadoslavL Nov 11 '21 at 10:53
  • Maybe this helps: https://stackoverflow.com/questions/43445942/opengl-glfw-undefined-reference-to-glfwinit – Jabberwocky Nov 11 '21 at 11:13

1 Answers1

1

I suggest to compile with a command similar to

gcc -Wall -Wextra -g $(pkg-config --cflags opengl glfw3) main.c \
     $(pkg-config --libs opengl glfw3) -o prog

Of course (before trying that) you need to install OpenGL related libraries, probably using (as root) the following command

aptitude install libopengl-dev libglfw3-dev

Be sure to read the documentation of GCC and the documentation of GDB and the documentation of binutils and the documentation of pkg-config.

PS. You could get inspiration from the source code of existing open source projects, such as GNU emacs or RefPerSys.

NB. You could contact me by email : basile@starynkevitch.net (home) or basile.starynkevitch@cea.fr (office, at CEA, LIST ....) near Paris in France.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Then please upvote or accept my answer. Feel free to email me at `basile@starynkevitch.net` (home) or `basile.starynkevitch@cea.fr` (office, at https://www-list.cea.fr/ ....) near Paris in France – Basile Starynkevitch Nov 11 '21 at 16:55