mingw 编译 glfw3 的 helloworld

glfw3 为基础开发 GUI 似乎是一个不错选项,有很多人尝试这么做了。今天也小试一把。

工具: mingw(不是 mingw-w64),头文件 GLFW/ ,库文件 glfw3.dll

需要注意,mingw-w64 的话,glfw3.dll 版本要匹配。需要用 mingw-w64 重新编译?

openGL 基本上 windows 都已经有安装了,不需要额外安装。(确认 C:WindowsSystem32opengl32.dll 存在)

代码:

#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 */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

编译指令:

gcc test.c glfw3.dll -lopengl32

第一次编译的时候,因为没有添加 -lopengl32,一直报错,找了好久:

undefined reference to `__imp_glClear'

添加 -lopengl32 之后就没问题了。

 =========================================

另外还有两个使用 glad 来载入 openGL 的例子。可以看下面:

helloworld

=========================================

关于 windows 下 openGL/GLFS/glu 会默认打开一个 cmd console,可以参考这个这里:

https://stackoverflow.com/questions/5995433/removing-console-window-for-glut-freeglut-glfw

我选取的是 FreeConsole() 这个方案,不过,需要在所有 include 的最前面添加 #include <Winsock2.h>。这个方案其实是在程序开始之初,直接把 console 释放掉,所以启动 GUI 程序时,会有黑影一闪而过。

=========================================

GLFS+IMGUI 的一个 demo :https://github.com/urddru/imgui-glfw

项目使用 cmake 来编译,可能需要小改点编译规则,需要 cmake 基础。

原文地址:https://www.cnblogs.com/pied/p/11389744.html