mingw 编译 glfw3 的 helloworld

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

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

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

openGL 基本上 windows 都已经有安装了,不需要额外安装。(确认 C:\Windows\System32\opengl32.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 基础。

posted @   Biiigfish  阅读(2171)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2018-08-22 Linux 端口转发一则
2018-08-22 LIN 笔记
2018-08-22 单片机 MCU 中 stack 使用的探讨
2018-08-22 调试 lvgl 的一个例子
点击右上角即可分享
微信分享提示