Ubuntu1604系统下的环境配置vscode和c++
这一部分,是通用的C++编译、debug配置,主要涉及到两个文件。
launch.json配置
这个文件主要是配置debug,具体设置如下:
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
task.json配置
可以点击Ctrl+Shift+P"调出命令面板,然后搜索Task,点击第一个模板,会提示你依据模板创建task.json,这里不管什么模板,随便选一个,然后直接复制粘贴以下设置代码:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.o",
],// 编译命令参数
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
需要关注到这段代码
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.o",
],// 编译命令参数
这就是g++编译命令,要注意最后一个参数" f i l e B a s e n a m e N o E x t e n s i o n . o " , , 必 须 跟 l a u n c h . j s o n 中 p r o g r a m 参 数 ( " {fileBasenameNoExtension}.o",,必须跟launch.json中program参数(" fileBasenameNoExtension.o",,必须跟launch.json中program参数("{workspaceFolder}/${fileBasenameNoExtension}.o")的文件名相对应。还有一点就是,如果要设置label参数的话,要将之设置为preLaunchTask的值。不记得preLaunchTask回头看看launch.json。这部分通用的C++编译运行设置就OK了,现在你可以愉快地用vscode写一波hello world。