如何在 Linux 中使用 GLFW 函数正确 link C++ 对象?

How to correctly link C++ objects using GLFW functions in Linux?

我创建了一个包含多个文件的非常简单的程序,现在我正尝试 link GLFW 库到它。我只将 GLFW 函数添加到文件 main.cpp 并且只在其中包含库,并且只使用命令 g++ main.cpp -lglfw 编译和执行该文件就可以了。

在我添加这个库之前,编译和 link 整个程序也很顺利,但是当我想 link 将所有东西放在一起时,即使其他文件中没有使用 GLFW 函数(g++ -Wall -g -std=c++11 -lglfw main.o hello_world.o console.o) 我使用的每个 GLFW 函数突然出现错误 'undefined reference to'。 (我在编译时没有出错 main.cpp:g++ -Wall -g -std=c++11 -lglfw -c main.cpp

这是文件 main.cpp:

#include "basis.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
// Open-GL
#include <GLFW/glfw3.h>
/// Setup:
/// sudo apt-get update
/// sudo apt-get install libglfw3
/// sudo apt-get install libglfw3-dev
/// Compile:
/// g++ main.cpp -lglfw

using namespace std;

void errorCallback(int error, const char* description) {
    fprintf(stderr, "Error: %s\n", description);
}

void test() {
    GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
    if (!window) {
        cout << "Window creation failed!" << endl;
    }
    runConsole();
}

int main(int argc, char* argv[]) {
    glfwSetErrorCallback(errorCallback);
    if (!glfwInit()) {
        cout << "GLFW initialization failed!" << endl;
        exit(EXIT_FAILURE);
    }

    test();

    glfwTerminate();
    return 0;
}

此外,我不知道库在我机器上的什么位置。此外,Linux 机器是 Virtual Box,源代码位于我的主机 windows 系统上的一个共享文件夹中,但在我在 [=31] 上编译它们之前,他从未对库造成问题=].

哎呀,在重新阅读问题时我想到了一个新的解决方案,并且有效:)正确的链接器命令是:g++ -Wall -g -std=c++11 main.o hello_world.o console.o -lglfw