Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确使用Linux中的GLFW函数链接C++对象?_C++_Linux_Linker Errors_Glfw_Undefined Reference - Fatal编程技术网

如何正确使用Linux中的GLFW函数链接C++对象?

如何正确使用Linux中的GLFW函数链接C++对象?,c++,linux,linker-errors,glfw,undefined-reference,C++,Linux,Linker Errors,Glfw,Undefined Reference,我已经创建了一个包含多个文件的非常简单的程序,现在我正在尝试将GLFW库链接到它。我只在main.cpp文件中添加了GLFW函数,并且只在那里包含了库,并且只有使用命令g++main.cpp-lglfw编译和执行这个文件才能正常工作 在我添加这个库之前,编译和链接整个程序也进行得很顺利,但是当我想将所有内容链接在一起时,即使在其他文件中没有使用GLFW函数,g++-Wall-g-std=c++11-lglfw main.o hello_world.o console.o我突然收到了错误“unde

我已经创建了一个包含多个文件的非常简单的程序,现在我正在尝试将GLFW库链接到它。我只在main.cpp文件中添加了GLFW函数,并且只在那里包含了库,并且只有使用命令g++main.cpp-lglfw编译和执行这个文件才能正常工作

在我添加这个库之前,编译和链接整个程序也进行得很顺利,但是当我想将所有内容链接在一起时,即使在其他文件中没有使用GLFW函数,g++-Wall-g-std=c++11-lglfw main.o hello_world.o console.o我突然收到了错误“undefined reference to”我使用的每个GLFW函数。编译main.cpp时没有出现错误:g++-Wall-g-std=c++11-lglfw-cmain.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机器是虚拟机,源代码位于我的主机windows系统上的共享文件夹中,但在我在Linux上编译库时,这从未给库带来问题。

Oops,在重读这个问题时,我想到了一个新的解决方案,它成功了:正确的链接器命令是:g++-Wall-g-std=c++11 main.o hello_world.o console.o-lglfw

如果安装了pkg config,则可以在链接命令末尾使用$pkg config glfw3-libs