参考步骤
- windows 需安装 MinGW, install gcc、g++、gdb 和 mingw32-make 并 进入到
MinGW\bin
文件夹下 把 mingw32-make.exe 修改成 make.exe
MinGW 不要下载 MinGW-w64因为后面可能会出现问题 MinGW,即 Minimalist GNU For Windows。它是一些头文件和端口库的集合,该集合允许人们在没有第三方动态链接库的情况下使用 GCC 产生 Win32 程序。
选择Windows pre-compiled binaries,因为我们使用的 MinGW 所以选择 32-bit Windows binaries
1 2
| > gcc ./src/glad.c -c -I ./include/ // 生成 .o文件 > ar -rc libglad.a glad.o // 生成我们所需要的 .a文件
|
将生成的 libglad.a 复制到 lib 文件下
- glm 复制到 include 目录下
- imgui 复制到 include 目下,Makefile 中添加以下命令
1 2 3 4
| SOURCES := $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS))) SOURCES += include/imgui/imgui_impl_glfw.cpp include/imgui/imgui_impl_opengl3.cpp SOURCES += include/imgui/imgui.cpp include/imgui/imgui_demo.cpp include/imgui/imgui_draw.cpp include/imgui/imgui_widgets.cpp
|
实例演示
1. Run 和 Clean
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <glad/glad.h> #include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void processInput(GLFWwindow *window);
const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600;
int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; }
while (!glfwWindowShouldClose(window)) { processInput(window); glfwSwapBuffers(window); glfwPollEvents(); }
glfwTerminate(); return 0; }
void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); }
void framebuffer_size_callback(GLFWwindow *window, int width, int height) { glViewport(0, 0, width, height); }
|
执行命令 run 编译和运行.exe 程序
1
| make run dir=01_test_glfw
|
执行命令 clean 删除 .exe 和对应文件夹下的.o 文件
1
| make clean dir=01_test_glfw
|
2. Debug
需要设置调试一个文件 修改 tasks.json
下面的条目
1 2 3 4 5 6 7 8 9 10 11 12 13
| "label": "build", "type": "shell", "group": { "kind": "build", "isDefault": true }, "windows": { "command": "powershell", "args": [ "-c", "make dir=01_test_glfw", ] },
|
注意
主要参考视频:VsCode下配置OpenGL开发环境
文字参考:VsCode下配置OpenGL开发环境GitHub
若按照上面视频出现包路径找不到,可以把对应的.a以及.h文件直接复制到MinGW路径下对应lib文件夹和include文件夹