今天用VS2019写项目的时候,突然给我疯狂报错,说运行时"DLL Initialization failed",我这项目完全没有任何动态库依赖,那意思你Windows自己的kernel都加载不好呗。
一气之下我决定换vscode写了,反正前端部分也是用vscode,后端能统一那是最好,查了半天,配置了一通终于搞好了,这里记录一下,也方便大家少走弯路。
VSCode本身只是一个编辑器,它是没有能力进行编译和调试的,所以需要额外装一个编译器。这里我推荐使用TDM_GCC,原因无他,一个安装包就全搞定了(记得让它自己添加环境变量),比MinGW方便点。Linux下你直接apt装一个g++就可以了。
这里安装结束后还需要做一个小修改,到TDM-GCC-64\bin
中找到mingw32-make.exe
,把它重命名成make.exe
,这样是为了和Linux下指令统一。
创建项目时,结构清晰总是没有错的。按照我个人的习惯,会把项目目录建成下面的格式:
├── include
├── src
├── lib
├── test
└── output
“Building Tool is The Key”,Makefile就是我选择的构建工具。虽然一般复杂一些的项目都是用CMake,但是作为Linux自带,GCC附带的make工具足够我们平时的项目使用了。
下面我给出一份Linux和Windows(需要安装TDM_GCC)通用的Makefile文件:
#
# 'make' build executable file 'main'
# 'make clean' removes all .o files
# 'make clean_all' removes all .o and executable files
# define debug/release mode
ver = release
# define Platform Architecture(32/64)
ARCH := 64
# define the Cpp compiler to use
CXX := g++
# define any compile-time flags
ifeq ($(ver), release)
CXXFLAGS := -std=c++17 -O3
else
CXXFLAGS := -std=c++17 -Werror -g
endif
# 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 program name
PROGRAM := main
# define output directory
OUTPUT := output
# define source directory
SRC := src
# define include directory
INCLUDE := include
# define lib directory
LIB := lib/lib$(ARCH)
ifeq ($(OS),Windows_NT)
MAIN := $(PROGRAM).exe
SOURCEDIRS := $(SRC) $(shell powershell "Get-ChildItem -Path $(SRC) -Recurse -Directory | Select-Object FullName")
INCLUDEDIRS := $(INCLUDE)
LIBDIRS := $(LIB)/win
FIXPATH = $(subst /,\,$1)
RM := del /q /f
MD := mkdir
else
MAIN := $(PROGRAM)
SOURCEDIRS := $(shell find $(SRC) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
LIBDIRS := $(shell find $(LIB)/linux -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:%/=%)) $(patsubst $(LIBDIRS)/lib%.a,-l%, $(wildcard $(LIBDIRS)/*.a))
# define the C source files
SOURCES := $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))
# define the C object files
OBJECTS := $(SOURCES:.cpp=.o)
#
# 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)
# 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)
# (see the gnu make manual section about automatic variables)
.cpp.o:
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
.PHONY: clean clean_all
clean:
$(RM) $(call FIXPATH,$(OBJECTS))
@echo Cleanup .o files complete!
clean_all:
$(RM) $(OUTPUTMAIN)
$(RM) $(call FIXPATH,$(OBJECTS))
@echo Cleanup all complete!
run: all
./$(OUTPUTMAIN)
@echo Executing 'run: all' complete!
这份Makefile文件是完全对应之前的项目结构,支持src和Include中继续设置子目录。
请确认安装了C/C++插件
VSCode虽然不能编译代码,但是它可以执行脚本,也就是自带的Task Runner,我们就是基于此来实现编译调试。
首先ctrl+shift+p
调出命令栏,输入task,应该可以看到任务:配置任务,回车之后会让你选择模板(取决于你当前打开的文件),我们不需要,随便选生成tasks.json
文件即可。打开替换成以下内容:
{
"tasks": [
{
"type": "shell",
"label": "build",
"command": "make ver=debug -j4",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
},
],
"version": "2.0.0"
}
这个文件是用来自定义任务的,我们这里定义了:
ctrl+shift+b
快速构建任务定义好之后需要有人来执行,VSCode左侧菜单栏的第四项Debug点击后,可以创建launch.json
文件。同样的,随便选模板,替换为以下内容:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/output/main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "E:\\Develop\\TDM-GCC-64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
这里我们定义了按下F5之后运行的流程:
好了,此时你只需要按下F5,或者在顶部菜单中选:运行->启动调试,就大功告成了。
ctrl+shift+p
,寻找C/C++: 编辑配置(JSON),然后修改。