如何在 Windows 上使用 gcc 链接到 GLFW?

问题描述 投票:0回答:1

我正在尝试在 Windows 上使用 GCC 链接到 GLWF。

我在计算机上本地构建了代码,获取了 include 和 lib 目录并删除了其余文件。现在我尝试使用批处理文件和 gcc 编译器将它们链接到我的代码。

build.bat:

@echo off

SET COMPILER_FLAGS= -I./Dependencies/GLFW/Include -I./Dependencies/GLFW/lib
SET LINKER_FLAGS= -lopengl32 -lglfw3 

gcc %COMPILER_FLAGS% -o foo.exe core/main.c %LINKER_FLAGS%

main.c:

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

文件路径:

错误

build.bat 中包含 -lglfw3 标志时,出现错误:

找不到-lglfw3

build.bat 中删除 -lglfw3 标志时,出现错误:

对 `glfwInit' 的未定义引用

对 `glfwCreateWindow' 的未定义引用

对 `glfwTerminate' 的未定义引用

对 `glfwMakeContextCurrent' 的未定义引用

对 `glfwSwapBuffers' 的未定义引用

对 `glfwPollEvents' 的未定义引用

对 `glfwWindowShouldClose' 的未定义引用

对 `glfwWindowShouldClose' 的未定义引用

对 `glfwTerminate' 的未定义引用

我到处都找过,也遇到过类似的问题,但无论我走到哪里,我真的不明白这个问题。我得到的最接近的是一篇有关 Arch Linux 的 Reddit 帖子,讨论链接器如何自动搜索以 lib 开头的文件夹以添加 .lib 文件,以及如何为 shell 脚本手动设置该文件夹。我尝试了一下,没有用,不知道是因为我使用的是windows而不是像unix系统那样的shell文件,所以它是一个bat文件,还是因为问题出在其他地方。

c gcc linker glfw
1个回答
0
投票

这个:

-I./Dependencies/GLFW/Include

告诉编译器搜索您

#include
中的头文件 您的源代码位于目录
./Dependencies/GLFW/Include
.

还有这个:

-I./Dependencies/GLFW/lib

还告诉它在

./Dependencies/GLFW/lib
中查找头文件 但你那里没有任何头文件。你已经(大概) 链接器需要的二进制库,包括(大概)
opengl32
glfw3
库。

你必须告诉

gcc
链接器应该在哪里搜索这些,并且你需要告诉它这是一个搜索的地方, 不是头文件。这样
gcc
就会将此信息传递给链接器 (
ld
),它需要它,而不是 C 编译器 (
cc1
),它不需要。 您可以使用
-L <dir>
选项来完成此操作。请参阅GCC手册:3.16 目录搜索选项

改变:

SET COMPILER_FLAGS= -I./Dependencies/GLFW/Include -I./Dependencies/GLFW/lib
SET LINKER_FLAGS= -lopengl32 -lglfw3

至:

SET COMPILER_FLAGS= -I./Dependencies/GLFW/Include -I./Dependencies/GLFW/lib
SET LINKER_FLAGS= -L./Dependencies/GLFW/lib -lopengl32 -lglfw3

幸运的是这个特殊问题:

cannot find -lglfw3

将会解决。

© www.soinside.com 2019 - 2024. All rights reserved.