博客链接:https://blog.xhyeax.com/2019/04/13/vscode-cpp-multiplefile/
之前从知乎抄的配置只能编译单文件源文件,编译多文件的话需要手动修改g++编译参数(不能忍啊→_→),于是搜索之,找到解决方案。
关键词:makefile
原本使用VSCode就可以正常进行编译调试等操作。
已安装make工具并添加到环境变量,我自己用的是mingw32-make
(也可使用msys提供的工具包,度盘链接: https://pan.baidu.com/s/1zJBcS6tjfXlwg1kP6VnLSg 提取码: f9ya)
我基本是直接复制参考博文里的模板(修改标准库为17),内容如下
# originating https://github.com/TheNetAdmin/Makefile-Templates
# tool marcros
CC := g++
CCFLAG := -std=c++17
DBGFLAG := -g
CCOBJFLAG := $(CCFLAG) -c
# path marcros
BIN_PATH := bin
OBJ_PATH := obj
SRC_PATH := src
DBG_PATH := debug
# compile marcros
TARGET_NAME := main
ifeq ($(OS),Windows_NT)
TARGET_NAME := $(addsuffix .exe,$(TARGET_NAME))
endif
TARGET := $(BIN_PATH)/$(TARGET_NAME)
TARGET_DEBUG := $(DBG_PATH)/$(TARGET_NAME)
MAIN_SRC := src/main.cpp
# src files & obj files
SRC := $(foreach x, $(SRC_PATH), $(wildcard $(addprefix $(x)/*,.c*)))
OBJ := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
OBJ_DEBUG := $(addprefix $(DBG_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
# clean files list
DISTCLEAN_LIST := $(OBJ) \
$(OBJ_DEBUG)
CLEAN_LIST := $(TARGET) \
$(TARGET_DEBUG) \
$(DISTCLEAN_LIST)
# default rule
default: all
# non-phony targets
$(TARGET): $(OBJ)
$(CC) $(CCFLAG) -o $@ $^
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.c*
$(CC) $(CCOBJFLAG) -o $@ $<
$(DBG_PATH)/%.o: $(SRC_PATH)/%.c*
$(CC) $(CCOBJFLAG) $(DBGFLAG) -o $@ $<
$(TARGET_DEBUG): $(OBJ_DEBUG)
$(CC) $(CCFLAG) $(DBGFLAG) $^ -o $@
# phony rules
.PHONY: all
all: $(TARGET)
.PHONY: debug
debug: $(TARGET_DEBUG)
.PHONY: clean
clean:
@echo CLEAN $(CLEAN_LIST)
@rm -f $(CLEAN_LIST)
.PHONY: distclean
distclean:
@echo CLEAN $(CLEAN_LIST)
@rm -f $(DISTCLEAN_LIST)
$@ --代表目标文件(target)
$^ --代表所有的依赖文件(components)
$< --代表第一个依赖文件(components中最左边的那个)
$? --代表比目标还要新的依赖文件列表
src文件夹下放源文件以及头文件,后面还要需要修改.vscode
中的task.json
和launch.json
注意:如果使用本文中的makefle,包含主函数的源文件名必须为main.cpp
如果你使用的是msys的工具包,那command就写make
按快捷键Ctrl+Shift+B
可以执行仅生成命令(build)
(在微软输入法中文模式时会出现表情窗口←_←)
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "mingw32-make",
"args": [
"default"
],
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
},
{
"label": "build-debug",
"command": "mingw32-make",
"args": [
"debug"
],
"type": "shell"
},
{
"label": "clean",
"command": "mingw32-make",
"args": [
"clean"
],
"type": "shell"
}
]
}
// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug/main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/debug/",
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "neverOpen",
"MIMode": "gdb",
"miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则去掉
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false,
}
],
"preLaunchTask": "build-debug" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
}
]
}
enjoy it!
?
VS Code 配置 C/C++ 环境