我正在尝试学习OpenGL,并且如果我直接从源代码对其进行编译,则能够编译我的项目。(编辑:非常重要的一点是,我只有在开始使用时才出现问题目标文件。)
但是,最近编译时间过长,因此我编写了一个makefile,该文件首先编译目标文件,并且仅在需要时更新它们。由于某些原因,我无法链接目标文件并返回以下错误:
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
不知何故,它们都与OpenGL有关,但我不知道为什么。 我上次遇到这种错误是因为我忘记告诉编译器要使用哪些静态库。但这一次似乎并非如此。
任何帮助将不胜感激!
以防万一,这是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);
}
编辑:在问这个问题之前,我认为这是由于C++ linkage引起的,但这似乎无法解决问题。