其实感觉vscode用于cpp环境不是很方便,比如昨天配置环境就搞了半天时间。
针对于cpp的配置项。有三个文件需要配置,分别是工程属性、编译、调试的配置文件:
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"browse": {
"path": [
"${workspaceFolder}",
"C:\\cygwin64"
],
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"${workspaceFolder}",
"C:\\cygwin64"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\cygwin64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build something",
"type": "shell",
"command": "g++",
"args":[
"${file}","-o","${fileBasenameNoExtension}.exe", "-g","-std=c++11"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build something"
}
]
}
最后直接ctrl+shift+b 编译就行了。点击F5编译+调试。
==============================
另外一篇帖子上转过来的配置变量,比较实用:
${workspaceRoot} 当前打开的文件夹
${file} 当前打开的文件
${relativeFile} 相对于workspaceRoot的相对路径
${fileBasename} 当前打开文件的文件名
${fileDirname} 所在的文件夹,是绝对路径
${fileExtname} 当前打开文件的拓展名,如.json
===============================
官方文档规则+本地实际编译器调试器路径来配置,网上乱找一通各种问题,最后还是跟着官方文档比较好:
https://code.visualstudio.com/docs/languages/cpp
Tips:调试的时候无法命中断点,那肯定就是像我一样忘记在编译命令上面添加 “-g”了。
感谢这个issue:https://github.com/Microsoft/vscode-cpptools/issues/416,不然我还在想着升级cpp扩展工具。