编译指令
参照g++ 的编译顺序: 从右边到左边, 所以很多情况下,如果编译出错,需要调整一下编译顺序.
否则就会出现,xxx找不到.
还有一点,需要注意:
linux 环境下,需要
创建软链接,实现opengl的库的搭建
sudo ln -s xxxxxxxxxxxx /usr/bin/libGL.so
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
// g++ main.cpp -std=c++17 -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi -o run
"./main.cpp",
"./glad.c",
"-fdiagnostics-color=always",
"-g",
"-o",
"main",
"-lglfw",
"-lvulkan",
"-ldl",
"-lpthread",
"-lX11",
"-lXxf86vm",
"-lXi",
"-lGL",
"-lXrandr",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
// 重绘 窗口
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
// 获取用户esc键
// 并且退出
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
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(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// 初始化glad
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// 回调函数
// 根据用户的当前的窗口大小,进行调整
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 循环窗口
// 不断的绘制窗口事件
while(!glfwWindowShouldClose(window))
{
// 注册用户按键函数
processInput(window);
// 检查并调用事件,交换缓冲
glfwSwapBuffers(window);
glfwPollEvents();
}
// 终止,并释放所有的资源
glfwTerminate();
return 0;
}