0

Source code:

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

Compiler:

g++ (MinGW.org GCC Build-20200227-1) 9.2.0

Commands I've tried and errors:

  1. g++ -I <pathHeaderFolder> Application.cpp -Wl, -BStatic -L <pathLibraryFolder> -lglu32 -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32:

    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find : Invalid argument
    collect2.exe: error: ld returned 1 exit status
    
  2. g++ Application.cpp -I <pathHeaderFolder>:

    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0x17): undefined reference to `glfwInit'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0x56): undefined reference to `glfwCreateWindow'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0x64): undefined reference to `glfwTerminate'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0x76): undefined reference to `glfwMakeContextCurrent'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0x81): undefined reference to `glfwWindowShouldClose'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0x96): undefined reference to `_imp__glClear@4'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0xa6): undefined reference to `glfwSwapBuffers'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0xab): undefined reference to `glfwPollEvents'
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Bruh\AppData\Local\Temp\cctDJEee.o:Application.cpp:(.text+0xb2): undefined reference to `glfwTerminate'
    collect2.exe: error: ld returned 1 exit status
    
  3. g++ Application.cpp -I <pathHeaderFolder> -Wl, -BStatic -<pathLibraryFolder>\libglfw3.a:

    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find : Invalid argument
    collect2.exe: error: ld returned 1 exit status
    
  4. g++ -I <pathHeaderFolder> Application.cpp -Wl, -BStatic -L <pathLibraryFolder> -libglfw3.a:

    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find : Invalid argument
    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -libglfw3.a
    collect2.exe: error: ld returned 1 exit status
    
  5. g++ -I <pathHeaderFolder> Application.cpp -Wl, -BStatic -L <pathLibraryFolder>libglfw3.a -mwindows:

    c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find : Invalid argument
    collect2.exe: error: ld returned 1 exit status
    
genpfault
  • 51,148
  • 11
  • 85
  • 139
Terys
  • 3
  • 2

2 Answers2

1

Spaces in arguments matter!

Here:

-Wl, -BStatic

Try removing the extra space:

-Wl,-BStatic

The reason is that the part after -Wl, is the argument passed to the linker.

See questions like I don't understand -Wl,-rpath -Wl, and `-Wl,` prefix to compiler flag

Acorn
  • 24,970
  • 5
  • 40
  • 69
0

OK, I tried deleting space but it didn't fix the problem. I was getting error:

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: unrecognized option '-BStatic'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: use the --help option for usage information
collect2.exe: error: ld returned 1 exit status

I was going through the gcc and linker documentation, but while not knowing how it works, finally tried and it worked:

g++ -I<pathHeaderFolder> Application.cpp -Wl,<pathLibraryFolder>libglfw3.a -lglu32 -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32

I don't really know why it worked, but I read in the linker documentation that if passing options to the linker it must be done through the -Wl

Also, adding "-L" before library path for linker

-Wl,-L<pathLibraryFolder>libglfw3.a

was giving me an error like I didn't link a library. I don't know why

But anyway it worked, so thanks a lot for help! I wouldn't come up that spaces in arguments matter

Terys
  • 3
  • 2