需求
写leetcode本地调试,在vscode工作区发现所有的.exe文件都生成在了和.cpp同目录的文件夹下,于是就显得非常杂乱没有调理。
于是参考这个文章.说是这是对 .exe 文件位置重定向后的工作区文件目录.
然后变成这样了,当然,现在这个目录下代码很少,所以不明显。
解决方法
创建相关配置文件
打开工作区所在文件夹。一般来说从头开始搭建,应该是个空文件夹是吧。
点击左侧的调试按钮->创建 launch.json 文件
选择 C++(GDB/LLDB)
选择gcc.exe-生成和调试活动文件(这个图我用的是别人博客的,因为我懒得在自己电脑截了。反正就mingw下gcc编译器的位置)
返回工作区文件目录,发现生成了.vscode文件夹,包含launch.json和tasks.json文件。简单了解下:tasks用于编译,launch用于执行编译后的文件。
重定向
对一般的项目来说,我们都喜欢把可执行文件放到 build 文件夹下
这边直接贴json文件内容好了,然后指出哪些地方要改
task.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
| { "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe 生成活动文件", "command": "D:\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\build\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ], "version": "2.0.0" }
|
接下来是在原始生成的基础上哪里进行了修改,其实上面都有注释了
1 2
| (1) "${fileDirname}\\${fileBasenameNoExtension}.exe" 改成 "${fileDirname}\\build\\${fileBasenameNoExtension}.exe" (2) "cwd": "${fileDirname}" 改成 "cwd": "${fileDirname}",也就是不改动 在[这篇文章](https://blog.csdn.net/m0_51269961/article/details/119681249)里让改成"cwd": "C:\\Program Files\\mingw64\\bin",但是我这边不成功。
|
对于第(2)条,在这篇文章里让改成”cwd”: “C:\Program Files\mingw64\bin”,但是我这边不成功。
launch.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
| { "version": "0.2.0", "configurations": [
{ "name": "(gdb) 启动", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\build\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", "preLaunchTask": "C/C++: g++.exe 生成活动文件", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "将反汇编风格设置为 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ] } ] }
|
在原始生成的文件上的改动为
1 2 3
| (1) "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 改成 "program": "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe",
|
不能忘记句尾的逗号 ‘,’
Code Runner
点击Edit in settings.json打开配置项,由于Code Runner中配置文件较多,我们只修改相关部分即可
1 2 3 4 5 6
| (1) "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", 替换成 "c": "cd $dir && gcc $fileName -o $workspaceRoot/build/$fileNameWithoutExt && $workspaceRoot/build/$fileNameWithoutExt", (2) "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", 替换成 "cpp": "cd $dir && g++ $fileName -o $workspaceRoot/build/$fileNameWithoutExt && $workspaceRoot/build/$fileNameWithoutExt",
|
然后就好啦,再运行cpp文件就会把 .exe 文件生成到 ./build 文件夹下啦,非常舒适。