0

I'm new to C++ and i'm try to learn to create my window so i watched a YouTube tutorial. And i followed what he did and i'm getting this errors.

here's my code.

#include "include/GLFW/glfw3.h"
#include "deps/linmath.h"
#include <stdlib.h>
#include <stdio.h>

int main(){

if (!glfwInit()) {
    exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

GLFWwindow* window = glfwCreateWindow(640, 480, "OPENGL EXAMPLE", NULL, NULL);
if(!window) {
    glfwTerminate();
    exit(EXIT_FAILURE);
}
glfwSwapInterval(1);

while(!glfwWindowShouldClose(window)) {

    //Setup View
    float ratio;
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    ratio = width / (float)height;
    glViewport(0, 0, width, height);
    glClear(GL_COLOR_BUFFER_BIT);

    glfwSwapBuffers(window);
    glfwPollEvents();

}

glfwDestroyWindow(window);
glfwTerminate;
exit(EXIT_SUCCESS);
}

and this is the error

/usr/bin/ld: /tmp/ccKjVhJ2.o: in function `main':
main.cpp:(.text+0x1c): undefined reference to `glfwInit'
/usr/bin/ld: main.cpp:(.text+0x3e): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x4d): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x71): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text+0x81): undefined reference to `glfwTerminate'
/usr/bin/ld: main.cpp:(.text+0x95): undefined reference to `glfwSwapInterval'
/usr/bin/ld: main.cpp:(.text+0xae): undefined reference to `glfwGetFramebufferSize'
/usr/bin/ld: main.cpp:(.text+0xe6): undefined reference to `glViewport'
/usr/bin/ld: main.cpp:(.text+0xf0): undefined reference to `glClear'
/usr/bin/ld: main.cpp:(.text+0xfc): undefined reference to `glfwSwapBuffers'
/usr/bin/ld: main.cpp:(.text+0x101): undefined reference to `glfwPollEvents'
/usr/bin/ld: main.cpp:(.text+0x10d): undefined reference to `glfwWindowShouldClose'
/usr/bin/ld: main.cpp:(.text+0x122): undefined reference to `glfwDestroyWindow'
collect2: error: ld returned 1 exit status
   

I don't understand why it's saying "Undefined Reference to 'glfwInit' "

  • It looks like you are not linking required library. – MikeCAT Jun 13 '22 at 14:27
  • Related: https://stackoverflow.com/questions/43445942/opengl-glfw-undefined-reference-to-glfwinit – mch Jun 13 '22 at 14:27
  • `glfwInit` is a function you call in your code (as you can see). *Undefined reference* means that when you built your program you failed to link with the library containing that function. That's what it means, how you fix it is another matter. I suggest reading some tutorials on the internet, but make sure that they are talking about **your** compiler and **your** operating system (gcc and linux presumably). – john Jun 13 '22 at 14:34
  • Can you link some tutorials? Thanks in advance – Chester232 Jun 13 '22 at 14:59
  • you must link the libraries when compiling like "gcc -o output filename.cpp -lglfw -lGL -lstdc++" – Chester232 Oct 01 '22 at 16:45

0 Answers0