【问题标题】:Linking glfw g++ [duplicate]链接glfw g++ [重复]
【发布时间】:2021-11-13 05:23:07
【问题描述】:

我一直在尝试在不使用 IDE 的情况下使用 OpenGL 进行编码,但是手动链接确实让我感到困惑。我有一个包含 libglfw3.a、glfw3.h 和包含此示例代码的 .cpp 文件的文件夹。

#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++ source.cpp -lglfw3 -o program

但我得到了这些错误。

C:\Users\murra\Coding\Manual>g++ source.cpp -lglfw3 -o proc
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x17): undefined reference to `glfwInit'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x56): undefined reference to `glfwCreateWindow'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x64): undefined reference to `glfwTerminate'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x76): undefined reference to `glfwMakeContextCurrent'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x81): undefined reference to `glfwWindowShouldClose'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0x96): undefined reference to `_imp__glClear@4'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0xa6): undefined reference to `glfwSwapBuffers'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0xab): undefined reference to `glfwPollEvents'
C:\Users\murra\AppData\Local\Temp\ccO7xs1Q.o:source.cpp:(.text+0xb2): undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status

我对这些东西很陌生,因此我们将不胜感激。

【问题讨论】:

  • 你可以试试:g++ -o proc -lglfw3 source.cpp
  • glfw 库是否为您正在使用的操作系统/编译器编译?

标签: c++ opengl g++ glfw


【解决方案1】:

当您链接第三方库时,您需要“告诉”编译器标头的位置以及库的位置。

通常,除了 -l 之外,还会使用 -I-L p>

您也可以使用 -I-L

将其减少到 2 个步骤

如果链接导致错误,IMO 有 3 个主要原因。

  1. 路径不正确
  2. 混合共享库和静态库标志(例如 - -MT-MD 在 VS 上)
  3. 您未包含的其他库依赖项。

在您的情况下,缺少的符号似乎来自其他库。尝试 -lglfw3 -lglew32 -lopengl32 -lgdi32 作为附加选项。显然这些需要在您的计算机上。

【讨论】:

  • 由于错误消息中提到的唯一非glfw方法是glClear,因此可能不需要glew或gdi32。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
相关资源
最近更新 更多