{"title": "VSCode\u914d\u7f6eC/C++\u5f00\u53d1\u73af\u5883", "update_time": "2022-01-03 16:18:00", "tags": "vscode", "pid": "355", "icon": "linux.png"}
## VSCode配置C/C++开发环境 ## 安装微软的2个插件 * C/C++ (C/C++ IntelliSense, debugging, and code browsing.) * C/C++ Extension Pack (Popular extensions for C++ development in Visual Studio Code.) 注意,这2个插件都是Microsoft出品的,不要装错了 ## 配置编译和Debug文件 * 位置是在workspace下的.vscode/tasks.json ``` { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "build", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": "build", "detail": "编译器: /usr/bin/g++" } ] } ``` * 位置是在workspace下的.vscode/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": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/hello", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "preLaunchTask": "C/C++: g++ 生成活动文件", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] } ``` 说明:launch.json和tasks.json是通过 launch.json中的preLaunchTask的值和tasks.json的label来关联的。 * launch.json 可以根据vscode的提示来生成 * tasks.json 可以通过 终端->配置任务来生成 ## Attach到一个已经在运行的程序 * .vscode/launch.json增加如下配置 `(gdb) Attach` 为新增的配置 ``` { // 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) Attach", "type": "cppdbg", "request": "attach", "program": "${workspaceFolder}/hello", "processId": "${command:pickProcess}", "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/hello", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "preLaunchTask": "C/C++: g++ 生成活动文件", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] } ``` 如果需要sudo才能gdb, 可以设置 miDebuggerPath ,这里把gdb的命令wrapper一下 /mypath/sudogdb 的内容如下 ``` sudo gdb ``` 在对应的config item里增加如下配置 ``` "miDebuggerPath": "/mypath/sudogdb" ``` 在第一次debug的时候可能会失败(因为vscode没法弹出sudo的密码框),弹出的终端里执行一次 `sudo -l`, 后面再debug就不需要再输入密码了 ## 远程开发模式问题记录 * #include
找不到的问题 如果是ubuntu的开发机,需要安装一下开发相关的软件包 `sudo apt-get install build-essential` * 当远程开发模式时,GDB Attach 的时候, pkexec提示没有权限问题 增加如下文件 /usr/share/polkit-1/actions/com.ubuntu.pkexec.gdb.policy 内容如下 ```
gdb-settings
yes
yes
yes
/usr/bin/gdb
true
``` ## 参考文档 * https://github.com/microsoft/vscode-remote-release/issues/2053