直接就能使用vscode编译并运行带有中文名的C++程序,在tasks.json里面自定义一个任务项就行。

tasks.json

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
{
"version": "2.0.0",
"tasks": [
{
"label": "Run C/C++ Program In Terminal Without Build",
"detail": "在终端运行C/C++程序,不编译",
"type": "shell",
"windows":{
"command": "start cmd /c \"\"${fileDirname}\\${fileBasenameNoExtension}.exe\" && pause\""
},
"linux":{
"command": "\"${fileDirname}/${fileBasenameNoExtension}\" && read -p \"请按任意键继续. . .\""
},
"presentation": {
"focus": true,
"close": true
},
"problemMatcher": []
},
{
"label": "G++ Build And Run In Terminal",
"detail": "使用G++编译并新开终端窗口运行",
"type": "shell",
"dependsOn":[
"G++ Build",
"Run C/C++ Program In Terminal Without Build"
],
"dependsOrder": "sequence",
"problemMatcher": []
},
{
"type": "shell",
"label": "G++ Build",
"command": [
"g++",
"-Wall",
"-w",
"-g3",
"-I",
".",
"\"${file}\"",
"-o",
"\"${fileDirname}/${fileBasenameNoExtension}\""
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"showReuseMessage": true,
}
}
]
}

效果演示

默认gdb编译中文文件会报错

image-20230826195508871

设置后如图可以正常编译运行

  1. Crtl +Shift + P调出命令面板搜索运行任务
  2. 如图所示选择编译运行
  3. 运行效果如图,实测可以编译运行

image-20230826200200315

image-20230826200113921

image-20230826200126421

你不能运行是因为你不单单是运行程序,而是调试程序,并且你还是使用gdb来进行调试,gdb不支持中文路径名,不是vscode)的问题。

如果你要在VScode中调试带中文名的C++程序,就使用VC的工具链进行编译,然后launch.json里面定义一个使用cppvsdbg的启动项,不过你要使用VC工具链的话,需要下载VS,感兴趣的可以看下我写的文章中关于使用MSVC配置vscode的部分,这里就不介绍了