Arch Linux 上的 OpenGL - 链接器错误:未定义的引用

OpenGL on Arch Linux - Linker Error: Undefined Reference

提问人:Electro 提问时间:8/4/2022 最后编辑:Rabbid76Electro 更新时间:8/4/2022 访问量:536

问:

我有一个问题。我最近开始从 YouTube 学习 OpenGL。在编译后的“组织”课程之后,我遇到了错误,例如未定义的引用。我已经比较了很长时间的代码,从原始存储库中复制它,但没有任何帮助。

主.cpp文件:

#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;
}

我使用命令编译代码,并看到以下错误:g++ main.cpp -o program glad.c -Wall -lGL -lGLU -lglut -lGLEW -lglfw -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor

/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

我将整个项目上传到 Github - https://github.com/Qeatrix/Learn_OpenGL

C++ 未定义引用

评论

0赞 Mathieu 8/4/2022
您应该单独编译所有文件并链接它们g++ -c -Wall foo.cpp -o foo.og++ *.o -lGL -lGLU -lglut -lGLEW -lglfw -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor
0赞 Mathieu 8/4/2022
考虑编写一个简单的 makefile

答:

0赞 Mathieu 8/4/2022 #1

你的编译不完整,你只尝试编译一个文件,而有几个。

您可以使用这些命令进行编译:

# 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
 

或者您可以使用一些: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)

并键入进行编译make

评论

2赞 user253751 8/4/2022
也允许一次编译所有文件:但这意味着编译器每次都编译所有文件。最好使用它,因为其目的是如果文件没有更改,它不会一次又一次地编译文件g++ main.cpp glad.c EBO.cpp shaderClass.cpp VAO.cpp VBO.cpp .......makemake