Liuxiaozhu's Blog

Recording, Sharing and Practicing

0%

需求

写leetcode本地调试,在vscode工作区发现所有的.exe文件都生成在了和.cpp同目录的文件夹下,于是就显得非常杂乱没有调理。
于是参考这个文章.说是这是对 .exe 文件位置重定向后的工作区文件目录.
然后变成这样了,当然,现在这个目录下代码很少,所以不明显。
avatar

解决方法

创建相关配置文件

打开工作区所在文件夹。一般来说从头开始搭建,应该是个空文件夹是吧。
点击左侧的调试按钮->创建 launch.json 文件
avatar
选择 C++(GDB/LLDB)
avatar
选择gcc.exe-生成和调试活动文件(这个图我用的是别人博客的,因为我懒得在自己电脑截了。反正就mingw下gcc编译器的位置)
avatar
返回工作区文件目录,发现生成了.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" //"${fileDirname}\\${fileBasenameNoExtension}.exe"{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
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\build\\${fileBasenameNoExtension}.exe", // "${fileDirname}\\${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中配置文件较多,我们只修改相关部分即可
avatar

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 文件夹下啦,非常舒适。

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.
LZJ: The above is generated automatically. And this page is a hello-world for hexo in the beginning. Honestly speaking, a page which not belongs to me but exists in my blog is annoying for me at first time. But on second thought, I can revise this article by adding more details or instructions, which might be easily forgot, to help me write the blog.

Quick Start

Create a new post

1
$ hexo new "My New Post"

Or

1
$ hexo new post "My New Post"

The “My New Post” is the title of my new post :P

More info: Writing

By the way, I turn on the “categories” tag in every post. So, if posible, add the following contents at the top of articles.

1
2
3
4
5
---
title: New Title
date: 2020-08-16 09:33:36
categories: New Category
---

Run server

1
$ hexo server

Or its abbreviation

1
$ hexo s

More info: Server

Generate static files

1
$ hexo generate

Or its abbreviation

1
$ hexo g

More info: Generating

Deploy to remote sites

1
$ hexo deploy

Or its abbreviation

1
$ hexo d

Clean cache and static file

1
$ hexo clean

This command is used to clean cache file db.json and static file public.

More info: Deployment

After I write a new article and want to post it

1
2
$ hexo clean
$ hexo g -d #等价于执行hexo g 后再执行一次 hexo d

After that, input the blog address in the browser “liuxiaozhu01.github.io”, and open the blog, then I will see the new article I wrote before :>

Encoding problem

Watch out the encoding problem. In Windows plantform, the system default encoding is GB2312(at least when I open a file or create a new file in VScode, it will be with GB2312 encoding. FUCK U, Microsoft! :)).

So, REMEMBER to open or save the file with encoding UTF-8, or the Chinese character will become garbled!!!