起因也是VS太笨重,我可怜的轻薄本很吃力,遂想使用VSC来实现OpenGL开发。
本文是参考一位大佬yocover而编写的,记录目的:防忘、方便下次配置、方便他人配置。
一、如何编译C/C++文件?
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 程序。
这里需要注意gcc版本需要9.2,如果是6.0可能会出一些问题,当然你可以使用我这个下载链接:https://wild-civil.lanzoum.com/iHRAp1f6oyfg 密码:27as。不想自己动手下载MinGW的, 可以往下翻看步骤④
① 安装MinGW
全程Next
即可(当然想修改安装路径自行修改即可)
② 选择相应的包进行下载
③ 搞一个make
在MinGW/bin
文件夹下,进行如下操作,方便直接使用make
命令
④ 添加环境变量!
↓LOOK AT HERE ↓
如果你觉得自己下载MinGW太麻烦,我这里也有→现成的←😋只需要进行步骤④↑可(解压然后复制bin目录进行环境配)。
当然我们可以在cmd中测试一下:
二、配置VsCode
①下载VsCode
如果你还没有VS Code?!那可太糟糕了,快去下载一个吧,网上教程很多喔。
②下载插件
VsCode需要安装插件C/C++
和 C/C++ Project Generator
如下图所示。
③创建C++工程
安装完毕之后,新建一个文件夹,进入文件夹,并在空白处右键,通过VsCode打开
进入VsCode后按Ctrl+Shift+P
,输入Create C++
,并单击如下图框住的选项,选择将项目构建在当前文件夹,即可实现C++项目的构建
④make run
紧接着按Ctrl+Shift+`,在终端中输入make run即可像下图一样!(注意注意,各种快捷键操作的前提是键盘是英文输入模式)
Nice!非常漂亮,注意!接下来,重头戏开始
三、GLFW
glfw 下载 Windows pre-compiled binaries
选择Windows pre-compiled binaries,因为我们使用的 MinGW 所以选择 32-bit Windows binaries 对于 Ubuntu,通过
sudo apt install libglfw3-dev libglfw3
安装 glfw
服务器在国外可能不稳定,下载不下来莫慌,点这里,我提供的是3.3.8版本。
将
glfw-3.3.8.bin.WIN32\glfw-3.3.8.bin.WIN32\include\
下的GLFW
文件,复制到vscode创建的C++工程下的include文件夹下。
将
glfw-3.3.8.bin.WIN32\glfw-3.3.8.bin.WIN32\lib-mingw
路径下的libglfw3.a
与libglfw3dll.a
两个文件,复制到VsCode创建的C++工程下的lib文件夹下。
将
glfw-3.3.8.bin.WIN32\glfw-3.3.8.bin.WIN32\lib-mingw
路径下的glfw3.dll
文件,复制到VsCode创建的C++工程下的output文件夹下。
小功告成
四、GLAD
- glad 在线服务 生成静态库
Then
老样子,下载不下来或懒得自己下载的点这里,下载下来后自行解压并进入解压后的文件夹内
分别输入如下两行命令
gcc ./src/glad.c -c -I ./include/ // 生成 .o文件
ar -rc libglad.a glad.o // 生成我们所需要的 .a文件
将生成的
libglad.a
复制到 lib 文件下
将
glad\include
路径下的两个文件夹,include文件夹下。
中功告成
五、示例
打开Makefile
文件
懒得找的也可以直接参看我的Makefile文件↓
#
# 'make' build executable file 'main'
# 'make clean' removes all .o and executable files
#
# define the Cpp compiler to use
CXX = g++
# define any compile-time flags
CXXFLAGS := -std=c++17 -Wall -Wextra -g
# define library paths in addition to /usr/lib
# if I wanted to include libraries not in /usr/lib I'd specify
# their path using -Lpath, something like:
LFLAGS =
# define output directory
OUTPUT := output
# define source directory
SRC := src
# define include directory
INCLUDE := include
# define lib directory
LIB := lib
Libraries := -lglad -lglfw3dll
ifeq ($(OS),Windows_NT)
MAIN := main.exe
SOURCEDIRS := $(SRC)
INCLUDEDIRS := $(INCLUDE)
LIBDIRS := $(LIB)
FIXPATH = $(subst /,\,$1)
RM := del /q /f
MD := mkdir
else
MAIN := main
SOURCEDIRS := $(shell find $(SRC) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
LIBDIRS := $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD := mkdir -p
endif
# define any directories containing header files other than /usr/include
INCLUDES := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))
# define the C libs
LIBS := $(patsubst %,-L%, $(LIBDIRS:%/=%))
# define the C source files
SOURCES := $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))
# define the C object files
OBJECTS := $(SOURCES:.cpp=.o)
# define the dependency output files
DEPS := $(OBJECTS:.o=.d)
#
# The following part of the makefile is generic; it can be used to
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#
OUTPUTMAIN := $(call FIXPATH,$(OUTPUT)/$(MAIN))
all: $(OUTPUT) $(MAIN)
@echo Executing 'all' complete!
$(OUTPUT):
$(MD) $(OUTPUT)
$(MAIN): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS) $(Libraries)
# include all .d files
-include $(DEPS)
# this is a suffix replacement rule for building .o's and .d's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file)
# -MMD generates dependency output files same name as the .o file
# (see the gnu make manual section about automatic variables)
.cpp.o:
$(CXX) $(CXXFLAGS) $(INCLUDES) -c -MMD $< -o $@
.PHONY: clean
clean:
$(RM) $(OUTPUTMAIN)
$(RM) $(call FIXPATH,$(OBJECTS))
$(RM) $(call FIXPATH,$(DEPS))
@echo Cleanup complete!
run: all
./$(OUTPUTMAIN)
@echo Executing 'run: all' complete!
紧接着我们将下面的代码复制到main.cpp
中
#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);
}
敲Ctrl+Shift+`,在终端中输入make run即可弹出窗口啦!按ESC即可退出运行。
加油!坚持!再进行最后的操作你就胜利了!
Tips:小技巧
找不到头文件?
对了这里还有小技巧,如果头文件红了,可以在.vscode/c_cpp_properties.json
中进行添加路径,如果没有该json文件,可以敲Ctrl+Shift+`然后输入下图的内容即可调处c_cpp_properties.json
想要多个main.cpp共存?
如果你向我一样正在学习OpenGL,但又不想一个劲的生成副本main.cpp然后注释main.cpp等,你可以这样做:
#
# 'make run dir=file_name' build executable file 'main'
# 'make clean dir=file_name' removes selected .o and executable files
#
# define the Cpp compiler to use
CXX = g++
# define any compile-time flags
CXXFLAGS := -std=c++17 -Wall -Wextra -g
# define library paths in addition to /usr/lib
# if I wanted to include libraries not in /usr/lib I'd specify
# their path using -Lpath, something like:
LFLAGS =
# define output directory
OUTPUT := output
# define source directory 运行时修改此处路径
SRC := src/$(dir) #// 传递 var 变量定义执行文件目录
CLEAN_SRC := src/$(dir)/*.o #// 删除所有.o文件
# define include directory
INCLUDE := include
# define lib directory
LIB := lib
ifeq ($(OS),Windows_NT)
LIBRARIES := -lglad -lglfw3dll
MAIN := main.exe
SOURCEDIRS := $(SRC)
INCLUDEDIRS := $(INCLUDE)
LIBDIRS := $(LIB)
FIXPATH = $(subst /,\,$1) #这里细节错误
RM := del /q /f
MD := mkdir
else
LIBRARIES := -lglad -lglfw -ldl -lpthread
MAIN := main
SOURCEDIRS := $(shell find $(SRC) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
LIBDIRS := $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD := mkdir -p
endif
# define any directories containing header files other than /usr/include
INCLUDES := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))
# define the C libs
LIBS := $(patsubst %,-L%, $(LIBDIRS:%/=%))
# define the C source files
SOURCES := $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))
# define the C object files
OBJECTS := $(SOURCES:.cpp=.o)
# define the dependency output files
DEPS := $(OBJECTS:.o=.d)
#
# The following part of the makefile is generic; it can be used to
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#
OUTPUTMAIN := $(call FIXPATH,$(OUTPUT)/$(MAIN))
all: $(OUTPUT) $(MAIN)
@echo Executing 'all' complete!
$(OUTPUT):
$(MD) $(OUTPUT)
$(MAIN): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBRARIES)
# include all .d files
-include $(DEPS)
# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file)
# -MMD generates dependency output files same name as the .o file
# (see the gnu make manual section about automatic variables)
.cpp.o:
$(CXX) $(CXXFLAGS) $(INCLUDES) -c -MMD $< -o $@
.PHONY: clean
clean:
$(RM) $(OUTPUTMAIN)
$(RM) $(call FIXPATH,$(OBJECTS))
$(RM) $(call FIXPATH,$(DEPS))
@echo Cleanup complete!
# 此处./src/$(dir) 传递main函数 argv 的参数
run: all
./$(OUTPUTMAIN) src/$(dir)/
@echo Executing 'run: all' complete!
然后你在命令行执行make run dir=file_name
就可以编译你选定的那个文件夹下的main.cpp (注:这里file_name为01)
在命令行执行make clean dir=file_name
就可以清除main.exe、main.d和main.o
六、视频教程
七、进阶
如果还要导入其他的库可以按照下面的步骤:
-
glm 复制到 include 目录下
-
imgui 复制到 include 目下,Makefile 中添加以下命令
# define the C source files 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
-
assimp 下载已编译好的文件 Assimp3-1-1_MinGW4-8-1_Win32.zip
对于 Ubuntu,通过
sudo apt install libassimp-dev
安装 assimp