最近想要实现头文件、源文件、main函数三者在三个不同文件夹的实现,查找了很多博客,对tasks.json, launch.json, c_cpp_properties.json相关配置优良进一步的理解,现总结如下:
实现的内容有:
- 头文件、源文件、main函数三者在三个不同文件夹(include、src、自定义)的分离包含
- .exe文件和.cpp文件分开,.exe文件单独在一个文件夹bin内
目录
c_cpp_properties.json(修改,直接粘贴即可):
tasks.json, launch.json文件:
简单了解下:tasks
用于编译,launch
用于执行编译后的文件。
生成方式:桌面新建文件夹用vscode打开后,在工作区新建一个文件夹,名字自定义,写一个hello,world的demo,如图:
然后,启动运行和调试,选择自己的编译器:
这里,我选择第一个:C++(GDB/LLDB)
然后选择g++.exe(C语言对应gcc)
然后,.vscode文件夹下会生成tasks.json, launch.json, 的文件,默认配置内容如下(配置会根据你用户settings.json的内容不同而有所差别)
tasks.json(原内容):
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "F:\\mingw\\810\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
launch.json(原内容):
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "F:\\mingw\\810\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
下面针对一些内容进行修改,修改的地方用“//”注释,vscode自动生成的(未修改)的后面为了便于理解,使用“/**/”进行注释
tasks.json(修改,直接粘贴即可):
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件", /*任务名称*/
"command": "F:\\mingw\\810\\mingw64\\bin\\g++.exe", /*本地编译器路径*/
"args": [ /*包含传给g++命令的参数,用于实现特定功能*/
"-fdiagnostics-color=always",
"-g", /*生成和调试有关的信息*/
"${file}", /*指定编译文件为当前文件*/
/*******下面这一部分根据自己建立多少源文件就加几个******/
"${workspaceFolder}\\src\\LinkedList.cpp",//加入动态连接的源文件
"-o", /*制定输出文件的路径和名称*/
/*"${fileDirname workspaceFolder}\\${fileBasenameNoExtension}.exe"*/ //删掉默认的这句
"${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe", //修改.exe文件生成的位置
"-I", //
"${workspaceFolder}\\include", // 头文件所在文件夹
//"${workspaceFolder}\\src",
"-fexec-charset=GBK" //utf-8转GBK格式,解决中文乱码问题
],
"options": {
//"cwd": "${fileDirname}" //删掉默认的这句
"cwd": "F:\\mingw\\810\\mingw64\\bin" //已执行程序或脚本的当前工作目录。如果省略,则使用代码的当前工作区根。
},
"problemMatcher": [
"$gcc"
],
"group": { /*包含很多task,归为group*/
"kind": "build", /*表明该组任务类型是构建*/
"isDefault": true /*表明此任务为此组任务中的默认任务*/
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new",
"showReuseMessage": true,
"clear": false
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
launch.json(修改,直接粘贴即可):
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{/*包含Debug配置*/
"name": "g++.exe - 生成和调试活动文件",/* 配置名称,将会在启动配置的下拉菜单中显示*/
"type": "cppdbg",/* 配置类型,这里只能为cppdbg */
"request": "launch", /* 请求配置类型,可以为launch(启动)或attach(附加) */
/*"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",*///删掉默认的这句
"program": "${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe", // 将要进行调试的程序的本地路径
"args": [], /* 程序调试时传递给程序的命令行参数,一般设为空即可 */
"stopAtEntry": false,/*设为true时程序将暂停在程序入口处,即在main处打上断点,一般设置为false */
"cwd": "${workspaceFolder}",// 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],/*环境变量,设置为空值*/
"externalConsole": true,// 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "F:\\mingw\\810\\mingw64\\bin\\gdb.exe",//改为自己电脑上MinGW的编译器地址即可
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
c_cpp_properties.json文件:
生成c_cpp_properties.json文件:
扩展程序会根据当前系统环境配置基本信息,因此有可能配置不完整,这时需要通过生成c_cpp_properties.json文件来配置缺少的信息:
ctrl+shift+P打开Command Palette,运行C/Cpp: Edit configurations...生成c_cpp_properties.json:
默认如下:
删除原有内容,粘贴下面代码:
c_cpp_properties.json(修改,直接粘贴即可):
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}",
"${workspaceFolder}",
"${workspaceFolder}\\include", //头文件所在文件夹
"${workspaceFolder}\\src" //源文件所在文件夹
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "F:/mingw/810/mingw64/bin/g++.exe"
}
],
"version": 4
}
工作区settings.json
默认如下:
工作区settings.json(修改,直接粘贴即可):
{
"C_Cpp.intelliSenseEngineFallback":"Disabled",
"C_Cpp.intelliSenseEngine": "Default",
//"C_Cpp.intelliSenseEngine": "Default",
//Default:默认开启搜索引擎感知上下文
// Disabled:关闭感知上下文搜索
//Tag Parser:用于其他文件搜索感知
"editor.insertSpaces": true,
"files.autoGuessEncoding": true,
"editor.detectIndentation": false,
"commentTranslate.hover.enabled": true,//开启悬停翻译,需要下载commentTranslate插件
"files.associations": {
"vector": "cpp",
"array": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"iostream": "cpp",
"*.tcc": "cpp"
},
}
用户区settings.json
用户区settings.json(修改,直接粘贴即可):
//用户(全局)设置
{
"editor.fontFamily": "Consolas, 'Courier New', monospace",//字体
//"editor.fontFamily": "Sarasa Nono CL", //另外一种字体
"editor.fontSize": 16,//字号大小
"explorer.confirmDelete": false,//删除文件时是否打开资源管理器询问
"files.encoding": "utf8",
//对Arduino的配置
"arduino.logLevel": "info",
"arduino.path": "F:\\arduino\\Arduino",
//code Runner的一些配置
//"code-runner.runInTerminal": true,
//"code-runner.saveFileBeforeRun": true,
//禁用VScode的更新
"update.enableWindowsBackgroundUpdates": false,
"update.mode": "none",
//对C++配置
//"c_cpp.updateChannel":"Insiders",
//"C_Cpp.defalt.compilerPath":"F:\\mingw\\810\\mingw32\\bin\\gcc.exe",
"C_Cpp.default.compilerPath": "F:\\mingw\\810\\mingw64\\bin\\g++.exe",
"C_Cpp.default.cppStandard": "c++14",
"C_Cpp.default.cStandard": "c11",
"C_Cpp.default.includePath": [
"F:\\mingw\\810\\mingw64\\lib"
],
"C_Cpp.default.intelliSenseMode": "gcc-x64",
"commentTranslate.targetLanguage": "zh-CN",
"git.autorefresh": false,
"git.autoRepositoryDetection": false,
"explorer.confirmDragAndDrop": false,
}
简要说明:
vscode 当前编辑状态
描述 | 值 |
---|---|
当前打开工作区的路径 (.vscode文件夹所在路径) | C:\Users\lumen\Desktop\project |
当前编辑器打开的文件路径 (正在编辑的文件) | C:\Users\lumen\Desktop\project\source\main.cpp |
预定义变量含义
变量 | 含义 | 值 |
---|---|---|
${workspaceFolder} | 工作区文件夹 | C:\Users\lumen\Desktop\project |
${workspaceFolderBasename} | 工作区文件夹基本名称 | project |
${file} | 文件 | C:\Users\lumen\Desktop\project\source\main.cpp |
${fileDirname} | 文件目录名 | C:\Users\lumen\Desktop\project\source |
${fileBasename} | 文件基本名称 | main.cpp |
{fileBasenameNoExtension} | 文件基本名称(无扩展名) | main |
${fileExtname} | 文件扩展名 | .cpp |
${relativeFile} | 相对文件 | source\main.cpp |
${relativeFileDirname} | 相对文件目录名 | source |
${lineNumber} | 行数 | 光标的行号 |
${selectedText} | 被选择的文本 | 在代码编辑器中选择的文本 |
${execPath} | 执行文件的路径 | a.exe 的位置 |
${defaultBuildTask} | 默认构建任务的名称 | |
${cwd} | 启动时任务运行器的当前工作目录 |