编译 GLFW 代码时参数无效?

Posted

技术标签:

【中文标题】编译 GLFW 代码时参数无效?【英文标题】:Invalid argument when compiling GLFW code? 【发布时间】:2020-04-27 21:29:38 【问题描述】:

源代码:

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

编译器:

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

我尝试过的命令和错误:

    g++ -I &lt;pathHeaderFolder&gt; Application.cpp -Wl, -BStatic -L &lt;pathLibraryFolder&gt; -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
    

    g++ Application.cpp -I &lt;pathHeaderFolder&gt;:

    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
    

    g++ Application.cpp -I &lt;pathHeaderFolder&gt; -Wl, -BStatic -&lt;pathLibraryFolder&gt;\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
    

    g++ -I &lt;pathHeaderFolder&gt; Application.cpp -Wl, -BStatic -L &lt;pathLibraryFolder&gt; -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
    

    g++ -I &lt;pathHeaderFolder&gt; Application.cpp -Wl, -BStatic -L &lt;pathLibraryFolder&gt;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
    

【问题讨论】:

【参考方案1】:

参数中的空格很重要!

这里:

-Wl, -BStatic

尝试删除多余的空间:

-Wl,-BStatic

原因是-Wl,之后的部分是传递给链接器的参数。

查看I don't understand -Wl,-rpath -Wl, 和`-Wl,` prefix to compiler flag 等问题

【讨论】:

【参考方案2】:

好的,我尝试删除空间,但没有解决问题。我收到错误:

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

我正在浏览 gcc 和链接器文档,但虽然不知道它是如何工作的,但最终尝试并它工作了

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

我真的不知道它为什么起作用,但我在链接器文档中读到,如果将选项传递给链接器,则必须通过 -Wl 完成

另外,在链接器的库路径前添加“-L”

-Wl,-L<pathLibraryFolder>libglfw3.a

给了我一个错误,就像我没有链接库一样。不知道为什么

但无论如何它都有效,非常感谢您的帮助!我不会在争论中提出空格问题

【讨论】:

以上是关于编译 GLFW 代码时参数无效?的主要内容,如果未能解决你的问题,请参考以下文章

替换编译错误:参数数量错误或属性分配无效

如何使用 GLFW 预编译的二进制文件编译 Visual Studio 2019 发行版

Mac 上的 GLFW 环境配置

使用 makefile 链接 GLFW 库时遇到问题

xcode 编译glfw , 导出.h

Linux QT GCC OpenGL GLEW - 向编译器添加参数