0

I'm trying to learn OpenGL and was able to compile my project if I compile them directly from the source code. (EDIT: Very important that I only started having problems once I started to use object files.)

However, recently the compile time has been a little too long so I wrote a makefile that compiles object files first and only update them if needed. For some reason, I can't link the object files and returns the following error:

Compiling executable -> ./bin/2D_GAME
g++ -std=c++11 -Wall -Wextra -lglfw3 -lGL -lXrandr -lXinerama -lXcursor -lXi -lXxf86vm -lX11 -lpthread -lrt -lm -ldl -o 2D_GAME obj/main.o obj/game.o obj/stb_image.o obj/graphics/texture/texture_manager.o obj/graphics/texture/texture_2D.o obj/graphics/system/renderer/2D_Map/renderer_map.o obj/graphics/system/sprite/sprite_loader.o obj/graphics/glad/glad.o obj/graphics/shader/shader.o obj/utils.o
/usr/bin/ld: obj/main.o: in function `processInput':
main.cpp:(.text+0x19): undefined reference to `glfwGetKey'
/usr/bin/ld: main.cpp:(.text+0x34): undefined reference to `glfwSetWindowShouldClose'
/usr/bin/ld: obj/main.o: in function `main':
main.cpp:(.text+0x66): undefined reference to `glfwInit'
/usr/bin/ld: main.cpp:(.text+0x75): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x84): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x93): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0xb4): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text+0xf2): undefined reference to `glfwTerminate'
/usr/bin/ld: main.cpp:(.text+0x10b): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: main.cpp:(.text+0x112): undefined reference to `glfwGetProcAddress'
/usr/bin/ld: main.cpp:(.text+0x188): undefined reference to `glfwSetFramebufferSizeCallback'
/usr/bin/ld: main.cpp:(.text+0x274): undefined reference to `glfwWindowShouldClose'
/usr/bin/ld: main.cpp:(.text+0x75c): undefined reference to `glfwSwapBuffers'
/usr/bin/ld: main.cpp:(.text+0x761): undefined reference to `glfwPollEvents'
/usr/bin/ld: main.cpp:(.text+0x76b): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make: *** [makefile:31: 2D_GAME] Error 1

Somehow, its all related to OpenGL but I can't figure out why. The last time I got this kind of error was because I forgot to tell the compiler what static libraries to use. But this time that doesn't seem to be the case.

Any help would be greatly appreciated!

Just in case, here is what is in main.cpp main.cpp:

#include <iostream>
extern "C" {
#include <glad/glad.h>
#include <GLFW/glfw3.h>
}
#include "shader.h"
#include "stb_image.h"
#include "texture_2D.h"
#include "sprite_loader.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

// window resize callback function
void
framebuffer_size_callback(GLFWwindow * window, int width, int height);

// user input test: Pressing ESC key will close the window
void
processInput(GLFWwindow * window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

int
main(int argc, char ** argv)
{
    // Initialize glfw
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow * window = glfwCreateWindow(800, 600, "2D_RPG", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW window." << std::endl;
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent(window);

    // Initialize GLAD
    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD." << std::endl;
        return 1;
    }
    glViewport(0, 0, 800, 600);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    Shader shader("./resources/shaders/vertex/test1.vs", "./resources/shaders/fragment/test1.fs");

    Texture2D texture;
    texture.generate("./resources/sprites/test/test0.jpg");

    Sprite_Loader sprite_loader(shader);

    shader.use();
    glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(800), static_cast<GLfloat>(600), 0.0f, -1.0f, 1.0f);
    glUniformMatrix4fv(glGetUniformLocation(shader.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

    // render loop
    while (!glfwWindowShouldClose(window)) {
        // input
        processInput(window);

        // render code
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        sprite_loader.draw(texture, glm::vec2(0, 0), glm::vec2(100, 100), 1.57f, glm::vec3(0.0f, 0.0f, 1.0f));
        sprite_loader.draw(texture, glm::vec2(100, 0), glm::vec2(100, 100), 1.57f, glm::vec3(0.0f, 1.0f, 0.0f));
        sprite_loader.draw(texture, glm::vec2(0, 100), glm::vec2(100, 100), 1.57f, glm::vec3(1.0f, 0.0f, 0.0f));
        sprite_loader.draw(texture, glm::vec2(100, 100), glm::vec2(300, 300), 0.0f, glm::vec3(1.0f, 1.0f, 1.0f));
        sprite_loader.draw(texture, glm::vec2(200, 0), glm::vec2(200, 100), 0.0f, glm::vec3(1.0f, 1.0f, 1.0f));
        sprite_loader.draw(texture, glm::vec2(0, 200), glm::vec2(100, 200), 0.0f, glm::vec3(1.0f, 1.0f, 1.0f));

        // check all events and swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
} // main

void
framebuffer_size_callback(GLFWwindow * window, int width, int height)
{
    glViewport(0, 0, width, height);
}

EDIT: Prior to asking this question, I thought this was because of C++ linkage but that doesn't seem to fix the problem.

funnypig run
  • 182
  • 2
  • 9
  • You probably didn't get the order of the linker input right. – πάντα ῥεῖ Apr 20 '19 at 07:01
  • Thanks for the reply! What do you mean by "order of the linker input"? Do my object files need to be given to the compiler in a particular order? – funnypig run Apr 20 '19 at 07:06
  • Of course their order matters. Check [this specific answer](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/24675715#24675715) from the duplicate. – πάντα ῥεῖ Apr 20 '19 at 07:09
  • I believe that is not the case. Because as I wrote in the question, I was able to compile it if it's directly from the source file. It only gives me the error if I use object files. But I might be missing something. – funnypig run Apr 20 '19 at 07:12
  • @πάνταῥεῖ actually, never mind. I've figured it out. But I think you could have been a little bit more kind... I followed your link and it mentioned the order of -l[libs..] being incorrect but not that you needed the object files to come **BEFORE** all the -l[libs..] I know it's kind of obvious to you but it wasn't for me so, thanks a lot. – funnypig run Apr 20 '19 at 09:27

0 Answers0