Vscode .json 设置(gcc/clang/linux_cmake/windows_cmake)
萌新互相学习,互相交流 OVO
gcc
launch.json (调试配置文件)
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\program files\\mingw64\\bin\\gdb.exe", //此处为用户需要根据调试器的存储路径修改
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal", // 会生成外部终端
"preLaunchTask": "g++",
}
]
}
tasks.json (任务编译文件)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "g++",
"type": "shell",
"command": "D:\\program files\\mingw64\\bin\\g++.exe", //此处为用户需要根据编译器的存储路径修改
"args": [
"-g",
"${file}",
// "${fileDirname}\\*.cpp", // 支持简单的多文件 (当前路径下所有*.cpp文件)
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\program files\\mingw64\\bin\\" // 下载编译器MinGW 的bin文件夹
},
"problemMatcher": [
"$gcc"
]
}
]
}
clang
launch.json (调试配置文件)
// launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${file}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,我一般设置为true
"cwd": "${workspaceRoot}", // 调试程序时的工作目录
"targetArchitecture": "x86_64", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
"externalConsole": true,
"internalConsoleOptions": "neverOpen",
"MIMode": "gdb", //调试器名称
"miDebuggerPath": "D:\\Program Files\\LLVM\\bin\\gdb.exe", //调试器路径
"preLaunchTask": "clang++", //和tasks.json的label值要相同
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json (任务编译文件)
// tasks.json
{
"version": "2.0.0",
"command": "clang++", // 要使用的编译器
"args": [ // 编译命令参数
"${file}", //要编译的文件名,你也可以改成 *.cpp 表示编译当前目录所有的cpp文件
"-o", //指定生成的程序名字
"${file}.exe", //这是你要生成的程序名字
"-Wall", // 开启额外警告
"-g", // 生成和调试有关的信息
"-static-libgcc", // 静态链接
"-fcolor-diagnostics", //彩色信息
"-w", //屏蔽警告
"--target=x86_64-w64-mingw", // 默认target为msvc,不加这一条就会找不到头文件
//以下都是链接库参数,需要链接什么库就加在这
"-lws2_32",
"-lIphlpapi",
"-lgdi32"
],
"tasks": [
{
"label": "clang++", // 任务名称,与launch.json的preLaunchTask相对应
"type": "shell",
"group": {
"kind": "build",
"isDefault": true // 设为false可做到一个tasks.json配置多个编译指令,需要自己修改本文件,我这里不多提
},
"presentation": {
"echo": true,
"reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never。具体参见VSC的文档
"focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译c和c++来说,设为true没有意义
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
}
}
],
"problemMatcher": {
"owner": "c",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
Windows_cmake
launch.json (调试配置文件)
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/mycalculator.exe", // 所需调试可执行文件的路径
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:/program files/LLVM/bin/gdb.exe", // 你懂的 调试器路径
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build"
}
]
}
tasks.json (任务编译文件)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {"cwd": "${workspaceFolder}/build"}, // 外部构建,需要在项目文件夹下新建build文件夹
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
".."
],
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command":"make",
"args": [
]
},
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn":[
"cmake","make"
]
}
]
}
linux_cmake
launch.json (调试配置文件)
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/mycmakeexe", //absolute path of program
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json (任务编译文件)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
".."
],
},
{
"label":"make",
"group": {
"kind": "build",
"isDefault": true
},
"command":"make",
"args": [
]
},
{
"label": "Build",
"dependsOrder": "sequence", // accoring to the list ordership execute the mission
"dependsOn":[
"cmake",
"make"
]
}
]
}
Tips (自定义头文件报错)
当 使用 VSCode 开发项目时,项目make成功,但是 include 自定义头文件下有红色报错。
需要修改 settings.json
顺便附上settings.json 配置 ()
// settings.json
{
"files.defaultLanguage": "cpp", // ctrl+N新建文件后默认的语言
"editor.formatOnType": true, // 输入时就进行格式化,默认触发字符较少,分号可以触发
"editor.snippetSuggestions": "top", // snippets代码优先显示补全
"code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
"code-runner.executorMap": {
"c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics -lws2_32 -liphlpapi -lgdi32 -w --target=x86_64-w64-mingw && $dir$fileNameWithoutExt",
"cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics -lws2_32 -liphlpapi -lgdi32 -w --target=x86_64-w64-mingw && $dir$fileNameWithoutExt"
}, // 设置code runner的命令行,点击右上角的运行跑的就是这些代码,里面的参数啥意思看tasks.json
"code-runner.saveFileBeforeRun": true, // run code前保存
"code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
"code-runner.clearPreviousOutput": true, // 每次run code前清空属于code runner的终端消息
"C_Cpp.clang_format_sortIncludes": false, // 格式化时调整include的顺序(按字母排序),这个别开,不信以后遇到问题你就会来关了
"C_Cpp.intelliSenseEngine": "Default", // 可以为Default或Tag Parser,后者较老,功能较简单。具体差别参考cpptools扩展文档
"C_Cpp.errorSquiggles": "Disabled", // 因为有clang的lint,所以关掉
"C_Cpp.autocomplete": "Disabled", // 因为有clang的补全,所以关掉
"clang.completion.enable": true,
"C_Cpp.dimInactiveRegions": false,
"clang.cflags": [ // 控制c语言静态检测的参数
"--target=x86_64-w64-mingw",
"-std=c11",
"-Wall"
],
"clang.cxxflags": [ // 控制c++静态检测时的参数
"--target=x86_64-w64-mingw",
"-std=c++17",
"-Wall",
"-I${workspaceFolder}/**" //若自定义头文件报错则修改这里
],
"files.associations": {
"ostream": "cpp",
"iostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"optional": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"chrono": "cpp",
"thread": "cpp",
"winsock2.h": "c",
"ws2tcpip.h": "c",
"windows.h": "c",
"stdio.h": "c",
"ctime": "cpp",
"iomanip": "cpp"
} // 效果效果比cpptools要好
}