GLFW link 无法在 C++ 中与 mingw32 一起正常工作(?)

GLFW link not working correctly with mingw32 in C++ (?)

我正在尝试从 GLFW 网页编译此示例:

http://www.glfw.org/documentation.html

#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 */

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

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

    glfwTerminate();
    return 0;
}

我使用的是 mingw32,我从这里 http://www.glfw.org/download.html.

下载了 32 位 windows 二进制文件

我把它们放在各自的文件夹中:

glfw3.dllC:\Programming\MinGW32\bin 中,副本在我的 main.cpp 文件夹中。

glfw3dll.alibglfw3.aC:\Programming\MinGW32\lib

GLFW folder 及其 .h 文件在 C:\Programming\MinGW32\include

编译所有我用这个:g++ -lglfw3dll -lopengl32 main.cpp -Wall -o main.exe

这是我得到的错误:

D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0xf): undefined reference to `glfwInit'
D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0x4e): undefined reference to `glfwCreateWindow'
D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0x5e): undefined reference to `glfwTerminate'
D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0x71): undefined reference to `glfwMakeContextCurrent'
D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0x7f): undefined reference to `glfwSwapBuffers'
D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0x84): undefined reference to `glfwPollEvents'
D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0x90): undefined reference to `glfwWindowShouldClose'
D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o:animation.cpp:(.text+0x9e): undefined reference to `glfwTerminate'
C:\Programming\MinGW32\bin/ld.exe: D:\Users\Jorge\AppData\Local\Temp\ccA4Wc7r.o: bad reloc address 0x20 in section `.eh_frame'
C:\Programming\MinGW32\bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

我可以看到 linker 无法使用 glfw link,但为什么呢?我在做什么?谢谢

你编译的命令有问题。应该是:

g++ main.cpp -lglfw3dll -lopengl32 -lgdi32 -Wall -o main.exe

这是因为您首先编译 main.cpp 然后 link 库。创建 OpenGL 上下文时也需要 gdi32 库。