简体   繁体   中英

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

Im trying to compile this example from the GLFW webpage:

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;
}

Im using mingw32, and I downloaded the 32-bit windows binaries files from here http://www.glfw.org/download.html .

I put them in the respectives folders:

glfw3.dll in C:\\Programming\\MinGW32\\bin and a copy in my main.cpp folder.

glfw3dll.a and libglfw3.a in C:\\Programming\\MinGW32\\lib

GLFW folder with its .h files in C:\\Programming\\MinGW32\\include

To compile all I use this: g++ -lglfw3dll -lopengl32 main.cpp -Wall -o main.exe

And this are the errors Im getting:

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

I can see that the linker is someway unable to link with glfw, but why ? and what am I doing worng ? Thanks

The command you used to compile is the problem. It should be:

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

This is because you first compile main.cpp and then link the libraries to it. The gdi32 library is also needed when creating OpenGL context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM