下载下来之后是个压缩包, 我们先在咱的项目文件夹的第三方依赖中新建一个叫glad的文件夹, 然后把压缩包解压到那里面.
cd ~/openGL/3rdparty
mkdir glad
~/openGL/3rdparty/glad
~/openGL/3rdparty
目录下是这样的据我所知ubuntu20.04有两种桌面环境: X11和Wayland, 这两种需要安装的东西不一样, 先看一下自己的是哪一种, 再分别安装
查看的命令:
echo $XDG_SESSION_TYPE
如果是X11,执行这个:
sudo apt install xorg-dev
如果是Wayland, 执行这个:
sudo apt install libwayland-dev libxkbcommon-dev wayland-protocols extra-cmake-modules
mkdir ~/glfw && cd ~/glfw
mkdir src && mkdir build
cmake -S ~/glfw/src -B ~/glfw/build
cd ~/glfw/build && make
现在依赖配好了, 咱浅写一个能跑就行的程序让它编译运行一下.
回到我们的工作区
cd ~/openGL/src && vim main.cpp
~/openGL/src/main.cpp
#include #include #include void framebufer_size_callback(GLFWwindow *_window, int _width, int _height);int main(){glfwInit();glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);GLFWwindow *win_handle = glfwCreateWindow(600, 600, "终于配好环境了感动+10086", NULL, NULL);if (win_handle == NULL){std::cout << "Failed to crate GLFW Window" << std::endl;glfwTerminate();return -1;}glfwMakeContextCurrent(win_handle);if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){std::cout << "Failed to initialize GLAD" << std::endl;return -1;}glViewport(0, 0, 600, 600);glfwSetFramebufferSizeCallback(win_handle, framebufer_size_callback);while (!glfwWindowShouldClose(win_handle)){glfwSwapBuffers(win_handle);glfwPollEvents();}glfwTerminate();return 0;}void framebufer_size_callback(GLFWwindow *_window, int _width, int _height){glViewport(0, 0, _width, _height);}
~/openGL/CMakeList.txt
cd ~/openGL && vim CMakeList.txt
cmake_minimum_required(VERSION 2.8)project(opengl_test)# Include files#gladinclude_directories(${PROJECT_SOURCE_DIR}/3rdparty/glad/include/)add_executable(main ${PROJECT_SOURCE_DIR}/src/main.cpp${PROJECT_SOURCE_DIR}/3rdparty/glad/src/glad.c)target_link_libraries(main glfw GL dl)
cd ~/openGL/build && cmake .. && make
./main
大功告成!