Search code examples
c++openglgccglfw

Invalid argument when compiling GLFW code?


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
    

Solution

  • 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