在CodeBlocks中链接GLFW

时间:2014-10-21 19:14:06

标签: opengl codeblocks glfw

CodeBlocks的GLFW项目已过时,仅适用于GLFW 2.7。我使用的是最新版本3.0.4,并尝试静态链接CodeBlocks(我希望我使用的是正确的术语)。如果有人告诉我如何一步一步地做到这一点,我会很高兴。如果可能的话,我还想创建一个空项目并手动完成所有操作。

这是我尝试运行的代码:

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

这就是我在“其他链接器选项”中的含义: -lmingw32 -lopengl32 -lgdi32 我还复制了包含CodeBlocks / MinGW / include中头文件的GLFW文件夹。

这几乎就是我所做的一切,我得到了以下构建日志:

-------------- Build: Debug in Initializing OpenGL (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe  -o "bin\Debug\Initializing OpenGL.exe" obj\Debug\main.o  -lmingw32 -lopengl32 -lgdi32  
obj\Debug\main.o: In function `main':
D:/Development/OpenGL/Initializing OpenGL/main.cpp:8: undefined reference to `glfwInit'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:12: undefined reference to `glfwCreateWindow'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:15: undefined reference to `glfwTerminate'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:20: undefined reference to `glfwMakeContextCurrent'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:28: undefined reference to `glfwSwapBuffers'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:31: undefined reference to `glfwPollEvents'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:23: undefined reference to `glfwWindowShouldClose'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:34: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:2)

jigger你的项目配置,以便mingw调用看起来像这样:

x86_64-w64-mingw32-g++.exe -Llib -o bin\Debug\glfw-proj.exe obj\Debug\main.o -lglfw3 -lopengl32 -lgdi32

Note the -lglfw3

步骤:

  1. Project -> Build options...
  2. 选择顶级配置(即不是调试或发布)
  3. Search directories -> Compiler标签中添加GLFW标题的路径(GLFW/glfw3.h和朋友)
  4. Search directories -> Linker标签中添加GLFW库文件(libglfw3.a和朋友)的路径
  5. Linker settings标签中添加3个链接库:glfw3opengl32gdi32