本文内容涉及 CMake mingw OpenGL VSCode
如有错误,欢迎指出
如有问题,欢迎交流
其中涉及到的安装资源及库资源
链接:https://pan.baidu.com/s/1zEGHZZpuP6l97vNo3Ck9AQ?pwd=1234
提取码:1234
配置GLFW GLAD
- 根据自己需要新建一个文件夹,重命名为OpenGL_VSCode(无严格要求,最好不要包含空格和中文)
a. 新建子目录include,用于存放头文件
b. 新建子目录lib,用于存放库文件 - 下载GLFW:Download | GLFW
a. 因为我mingw编译器已经配置好了, 是64位的,因此图片中下载的库也是64位的, 具
体取决于个人情况和需求
b. 将 glfw-3.4.bin.WIN64\glfw-3.4.bin.WIN64\include 路径下的 GLFW 拷贝至
OpenGL_VSCode\include
c. 将 glfw-3.4.bin.WIN64\lib-mingw-w64 下的库文件拷贝至 OpenGL_VSCode\lib


- 下载GLAD:glad.dav1d.de
a. 将 glad\include 下的两个文件夹 glad / KHR 拷贝至 OpenGL_VSCode/include
b. 生成目标文件
c. 生成静态库
d. 将静态库文件拷贝至 OpenGL_VSCode/lib


生成静态库
# 生成目标文件
# 生成静态库
# 进入到 glad\src 目录中, 该步骤省略, 请自行操作
# 查看目前有哪些文件
# 可以看到目前只有.c文件
PS D:\username\Downloads\glad\src> ls
Directory: D:\username\Downloads\glad\src
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/9/2024 3:48 PM 112859 glad.c
# -I ../include 指明头文件所在目录
# 因为 glad.c 中的头文件包含形式为 #include <glad/glad.h>, 因此 include就是头文件所在目录
# 我用的是g++编译, gcc没有测试, 理论而言应该也是可行的
PS D:\username\Downloads\glad\src> g++ -c glad.c -I ../include -o glad.o
PS D:\username\Downloads\glad\src> ls
Directory: D:\username\Downloads\glad\src
# 生成了目标文件 glad.o
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/9/2024 3:48 PM 112859 glad.c
-a---- 9/11/2024 11:09 AM 81399 glad.o
# 将目标文件 glad.o 打包为静态库文件
PS D:\username\Downloads\glad\src> ar -rc libglad.a glad.o
PS D:\username\Downloads\glad\src> ls
Directory: D:\username\Downloads\glad\src
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/9/2024 3:48 PM 112859 glad.c
-a---- 9/11/2024 11:09 AM 81399 glad.o
-a---- 9/11/2024 11:10 AM 102220 libglad.a

配置 CMake mingw
- 下载
- 解压, 放到合适的路径下
- 修改环境变量PATH
- 在VSCode中下载cmake、cmakeTool 扩展
这部分b站上有许多Up讲的很明白, 就不再赘述了
需要再次强调一下, 如果你的库文件是32位的, 那么cmake mingw 同样下载32位, 反之亦然
此外,还需要在CMakeLists.txt 中设置库文件、头文件的路径,简易的配置如下
cmake_minimum_required(VERSION 3.30)
project(TEST)#项目名称
# 获取 include lib 所在目录,告诉编译器去哪里寻找头文件和库文件
get_filename_component(rootDir "${PROJECT_SOURCE_DIR}" PATH)#或DIRECTORY
get_filename_component(rootDir "${rootDir}" PATH)#或DIRECTORY
include_directories(${rootDir}/include)
link_directories(${rootDir}/lib)
add_compile_options(-Wall -std=c++11)# 这里根据自己需求设置
add_executable(test test.cc)
# 链接静态库(或者动态库,二者取其一)
target_link_libraries(test glad glfw3)
测试代码
把测试代码粘贴到test.cc即可
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
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);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
参考资源
本文参考了以下文章或视频
手把手教会VSCode的C++环境搭建,多文件编译,Cmake,json调试配置( Windows篇)_哔哩哔哩_bilibili

3万+

被折叠的 条评论
为什么被折叠?



