0%

vscode 调试 C++

添加配置, 设置.
launch脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
// 使用 IntelliSense 了解相关属性.
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/bin/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "Build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

其中的task脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskname": "build",
"label": "Build",
"command": "g++-5",
"args": [
"-g",
"-Wall",
"-std=c++14",
"${file}",
"-lpthread",
"-o",
"${workspaceRoot}/bin/main"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": "absolute",
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"label": "Run",
"type": "shell",
"dependsOn": "Build",
"command": "${workspaceRoot}/bin/main",
"args": [],
"presentation": {
"reveal": "always",
"focus": true
},
"problemMatcher": [],
"group": {
"kind": "test",
"isDefault": true
}
}
]
}
}

其中build部分可以替换为makefile. 如果是多文件编译, 可以使用make all命令, 编译单一cpp(含main)的文件, 使用make $(file)进行替换.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 描述: multifile 项目 makefile文件
# 版本: v2.0
# 修改记录: 1.先测试普通的cpp文件的编译运行
# 2.使用变量来改进我们的makefile文件
# 3.新加了一个源文件
# 4.使用伪目标,加上clean规则
# 5.使用wildcard函数,自动扫描当前目录下的源文件
# 6.加入了自动规则依赖
# 7.实现了opencv程序的编译
# 定义了可执行文件变量
BIN := bin
OUT := out
CFLAGS := -std=c++14


# 传递了文件参数
FILE := $(FILE)

# 定义了源文件列表变量
ifeq ($(strip $(FILE)), )
sources := $(wildcard *.cpp)
target:= all
else
sources := $(FILE)
target:=$(notdir $(basename $(sources)))
endif

executable := $(BIN)/$(target)
executable_main := $(BIN)/main

SRC := $(notdir $(sources))

# 使用变量的引用替换,定义了object文件列表
objects := $(addprefix $(OUT)/, $(SRC:.cpp=.o))

# 使用变量引用替换,定义依赖文件列表
deps := $(SRC:.cpp=.d)
# 定义编译命令变量
CC := g++-5
rm := rm -rf

#需要调用的链接库
LIBS := -lpthread

# 头文件路径
INCLUDE := -I.

# 链接库路径
LDFLAGS := -L/usr/local/lib
# 终极目标规则,生成可执行文件



$(executable) : $(objects)
-mkdir bin
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
mv $(executable) $(executable_main)

$(objects): %.o: $(sources)
-mkdir $(OUT)
$(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)

all : $(executable)



#clean规则
.PHONY: clean
clean:
#清除编译生成的所有文件
#$(RM) $(executable) $(objects) $(deps)
#清除编译生成的所有文件,不包括可执行文件
$(RM) $(objects) $(deps) $(executable)

# 自动规则依赖
deps := $(addprefix $(OUT)/, $(SRC:.cpp=.d))
sinclude $(deps)
$(deps): %.d: $(sources)
$(CC) -MM $(CFLAGS) $< > $@