vscode使用
vscode官网:https://code.visualstudio.com/,https://code.visualstudio.com/docs。
一、vscode安装
可直接在官网上下载windows或Linux安装包安装,也可Ubuntu通过命令行导入仓库安装。参考:https://code.visualstudio.com/docs/setup/linux
sudo apt install ./<file>.deb
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg sudo install -o root -g root -m 644 packages.microsoft.gpg /etc/apt/trusted.gpg.d/ sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/trusted.gpg.d/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list' sudo apt install apt-transport-https sudo apt update sudo apt install code # or code-insiders
一般安装完成后,基于不同语言开发时,需要安装不同插件,根据提示安装即可。
二、vscode远程开发
如果是windows+Linux虚拟机开发模式,在windows下安装vscode,配置远程开发,可远程Linux编程。
1. 点击扩展按钮,搜索Remote Development,并安装。
2. 配置远程服务器。点击远程资源管理器,点击齿轮图标,打开config文件,配置host ip,user等信息。或者选择ssh Targets,添加ssh主机。
3. 点选远程连接条目,Connection to host in New Window,输入密码远程链接。
4. 可进一步配置windows ssh公钥,实现免密连接。(可不配置)
三、vscode标准配置
vscode工程一般包含.vscode目录,用于存放工程配置文件,默认包含launch.json和tasks.json。
launch.json:用于配置程序启动项,常用的是配置启动(命令行)参数(args)。标准的参考文件:
{ // 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++ - Build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": ["hw:0,0", "atest.aac"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file", "miDebuggerPath": "/usr/bin/gdb" } ]
其中,name,program,cwd,miDebuggerPath,miDebuggerServerAddress需要根据实际情况配置。
注:如果嵌入式远程调试,需要配置miDebuggerPath和miDebuggerServerAddress:
"miDebuggerPath": "/home/zuozhongkai/linux/IMX6ULL/tool/gdb/bin/arm-linux-gnueabihf-gdb", "miDebuggerServerAddress": "192.168.1.251:2001"
task.json:用于配置编译选项,常用的是配置编译链接参数(args)。
{ "tasks": [ { "type": "shell", "label": "C/C++: g++ build active file", "command": "/usr/bin/gcc", "args": [ "-g", "${file}", "-lavformat", "-lavcodec", "-lavdevice", "-lavutil", "-lswscale", "-lswresample", "-lm", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ], "version": "2.0.0" }
compile_commands.json配置includePath环境
1) 使用cmake导出compile_commands.json
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=YES ..
或cmakelists.txt中
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2)vscode中使用ctrl++shift+P打开Command Palette,运行C/Cpp: Edit configurations(JSON)
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c11", "cppStandard": "gnu++14", "intelliSenseMode": "linux-gcc-x64", "compileCommands": "${workspaceFolder}/src/build/compile_commands.json" } ], "version": 4 }
在.vscode/c_cpp_properties.json中添加compileCommands字段。
也可在includePath中分贝指定头文件路径。
四、ROS开发
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes
五、快捷键
- ctrl + shift + p 命令行
- ctrl + p快速打开文件
- ctrl + space 强制弹出代码提示
六、技巧
1. vscode CMake管理时不能编译,要在CMakeLists.txt中增加Debug:
cmake_minimum_required(VERSION 3.0.2) project(sound_player) set(CMAKE_BUILD_TYPE Debug) ## Compile as C++11, supported in ROS Kinetic and newer add_compile_options(-Wall -std=c++11)
七、错误收集
1. 远程连接不成功
Could not establish connection to "192.168.3.xxx","试图写入的管道不存在" "install termal","Error: Running the contributed command: '_workbench.downloadResource' failed"等
关闭vscode重试几次,不成功,则参考:
vscode配置远程开发环境失败_workbench.downloadResource
原因:内网,无法下载
解决:找到报错日志里面的COMMIT_ID
2aeda6b18e13c4f4f9edf6667158a6b8d408874b
外网下载
https://update.code.visualstudio.com/commit:2aeda6b18e13c4f4f9edf6667158a6b8d408874b/server-linux-x64/stable
下载完毕后,将下载的文件upload到Remote Server的~/.vscode-server/bin/xxx文件夹下,同时删除其他文件,最后再使用tar -xvf vscode-server-linux-x64.tar.gz --strip-components 1解压文件
参考: