GLFW链接不能正常工作与mingw32在c++ (?)

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

本文关键字:mingw32 c++ 不能 链接 常工作 GLFW 工作      更新时间:2023-10-16

我试图从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,我从这里下载了32位windows二进制文件http://www.glfw.org/download.html.

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

C:ProgrammingMinGW32bin中的glfw3.dll和我的main.cpp文件夹中的副本。

C:ProgrammingMinGW32lib中的glfw3dll.alibglfw3.a

GLFW folder及其。h文件在C:ProgrammingMinGW32include

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

下面是我得到的错误:

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

我可以看到链接器在某种程度上无法与glfw链接,但为什么?我做错了什么?由于

您用来编译的命令出了问题。应该是:

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

这是因为您首先编译main.cpp,然后将库链接到它。gdi32库在创建OpenGL上下文时也是需要的。