Home > Software engineering >  OpenGL on Arch Linux - Linker Error: Undefined Reference
OpenGL on Arch Linux - Linker Error: Undefined Reference

Time:08-05

I have a problem. I recently started learning OpenGL from YouTube. After the lesson "Organizing" after compiling I got errors like an undefined references. I have been comparing code for a long time, copying it from original repositories, but nothing helped.

main.cpp file:

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <math.h>
#include <stdio.h>

#include "shaderClass.h"
#include "VBO.h"
#include "VAO.h"
#include "EBO.h"



// Vertices coordinates
GLfloat vertices[] =
{
    -0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f, // Lower left corner
    0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f, // Lower right corner
    0.0f, 0.5f * float(sqrt(3)) * 2 / 3, 0.0f, // Upper corner
    -0.5f / 2, 0.5f * float(sqrt(3)) / 6, 0.0f, // Inner left
    0.5f / 2, 0.5f * float(sqrt(3)) / 6, 0.0f, // Inner right
    0.0f, -0.5f * float(sqrt(3)) / 3, 0.0f // Inner down
};

// Indices for vertices order
GLuint indices[] =
{
    0, 3, 5, // Lower left triangle
    3, 2, 4, // Lower right triangle
    5, 4, 1 // Upper triangle
};



int main()
{
    // Initialize GLFW
    glfwInit();

    // Tell GLFW what version of OpenGL we are using
    // In this case we are using OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    // Tell GLFW we are using the CORE profile
    // So that means we only have the modern functions
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Create a GLFWwindow object of 800 by 800 pixels, naming it "YoutubeOpenGL"
    GLFWwindow* window = glfwCreateWindow(800, 800, "YoutubeOpenGL", NULL, NULL);
    // Error check if the window fails to create
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    // Introduce the window into the current context
    glfwMakeContextCurrent(window);

    //Load GLAD so it configures OpenGL
    gladLoadGL();
    // Specify the viewport of OpenGL in the Window
    // In this case the viewport goes from x = 0, y = 0, to x = 800, y = 800
    glViewport(0, 0, 800, 800);



    // Generates Shader object using shaders defualt.vert and default.frag
    // Shader shaderProgram("default.vert", "default.frag");



    // Generates Vertex Array Object and binds it
    VAO VAO1;
    VAO1.Bind();

    // Generates Vertex Buffer Object and links it to vertices
    VBO VBO1(vertices, sizeof(vertices));
    // Generates Element Buffer Object and links it to indices
    EBO EBO1(indices, sizeof(indices));

    // Links VBO to VAO
    VAO1.LinkVBO(VBO1, 0);
    // Unbind all to prevent accidentally modifying them
    VAO1.Unbind();
    VBO1.Unbind();
    EBO1.Unbind();



    // Main while loop
    while (!glfwWindowShouldClose(window))
    {
        // Specify the color of the background
        glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
        // Clean the back buffer and assign the new color to it
        glClear(GL_COLOR_BUFFER_BIT);
        // Tell OpenGL which Shader Program we want to use
        // shaderProgram.Activate();
        // Bind the VAO so OpenGL knows to use it
        VAO1.Bind();
        // Draw primitives, number of indices, datatype of indices, index of indices
        glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, 0);
        // Swap the back buffer with the front buffer
        glfwSwapBuffers(window);
        // Take care of all GLFW events
        glfwPollEvents();
    }



    // Delete all the objects we've created
    VAO1.Delete();
    VBO1.Delete();
    EBO1.Delete();
    // shaderProgram.Delete();
    // Delete window before ending the program
    glfwDestroyWindow(window);
    // Terminate GLFW before ending the program
    glfwTerminate();
    return 0;
}

I compile the code with the command g main.cpp -o program glad.c -Wall -lGL -lGLU -lglut -lGLEW -lglfw -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor and I see such errors:

/usr/bin/ld: /tmp/ccUbCcD3.o: in function `main':
main.cpp:(.text 0xed): undefined reference to `VAO::VAO()'
/usr/bin/ld: main.cpp:(.text 0xf9): undefined reference to `VAO::Bind()'
/usr/bin/ld: main.cpp:(.text 0x114): undefined reference to `VBO::VBO(float*, long)'
/usr/bin/ld: main.cpp:(.text 0x12f): undefined reference to `EBO::EBO(unsigned int*, long)'
/usr/bin/ld: main.cpp:(.text 0x147): undefined reference to `VAO::LinkVBO(VBO&, unsigned int)'
/usr/bin/ld: main.cpp:(.text 0x153): undefined reference to `VAO::Unbind()'
/usr/bin/ld: main.cpp:(.text 0x15f): undefined reference to `VBO::Unbind()'
/usr/bin/ld: main.cpp:(.text 0x16b): undefined reference to `EBO::Unbind()'
/usr/bin/ld: main.cpp:(.text 0x1b2): undefined reference to `VAO::Bind()'
/usr/bin/ld: main.cpp:(.text 0x205): undefined reference to `VAO::Delete()'
/usr/bin/ld: main.cpp:(.text 0x211): undefined reference to `VBO::Delete()'
/usr/bin/ld: main.cpp:(.text 0x21d): undefined reference to `EBO::Delete()'
collect2: error: ld returned 1 exit status

I uploaded my entire project to Github - https://github.com/Qeatrix/Learn_OpenGL

CodePudding user response:

Your compilation is not complete, you only try to compile one file, whereas there is several.

You can use theses command to compile :

# compile the C file
gcc -c -Wall glad.c -I lib/include/

# compile the c   files 
g   -c -Wall -I lib/include EBO.cpp
g   -c -Wall -I lib/include main.cpp
g   -c -Wall -I lib/include shaderClass.cpp
g   -c -Wall -I lib/include VAO.cpp
g   -c -Wall -I lib/include VBO.cpp

# link the object files
g   -o demo EBO.o glad.o main.o shaderClass.o VAO.o VBO.o -lglfw -ldl
 

Or you could use some Makefile:

CFLAGS= -C -Wall  -I lib/include
CXXFLAGS= -C -Wall  -I lib/include
LDFLAGS= -lglfw -ldl

CC=gcc
CXX=g  

all: demo

.PHONY: clean

clean:
    rm *.o

glad.o: glad.c 
    $(CC) $(CFLAGS) $< -c -o $@

%.o: %.c 
    $(CXX) $(CXXFLAGS) $< -c -o $@

demo: EBO.o glad.o main.o shaderClass.o VAO.o VBO.o
    $(CXX) $^ -o $@ $(LDFLAGS)

and type make to compile

  • Related