一、VS Code 及 C++ 开发组件安装
1、下载Visual Studio Code 编译器
官网下载ubuntu的deb安装包
sudo dpkg -i xxx.deb
2、汉化
自带的商店下载的插件,快捷键:Ctrl+Shift+x,搜索Chinese (Simplified) Language Pack for Visual Studio Code,点击下载,根据提示重启即可完成汉化
如果失败也可以手动设置:快捷键Ctrl+Shift+p,在选框中输入configure display language,点击确定,在出现的locale.json文件中,将"locale":“en” 改为“locale”:“zh-CN”,然后重启
3、C++ 组件安装
VS Code自带的商店下载的插件,快捷键:Ctrl+Shift+x,下载各种依赖包,包括:c/c++,c/c++ clang command adapter,c++ intellisense,CMake和CMake Tools
二、VSCode CMake 编译 C++ 工程
对于C++工程,有四个json
文件需要配置,创建的方式分别是:
c_cpp_properties.json
:配置项目结构引入所需要的外部头文件,自动生成和更新,输入C/C++:Edit configurationtasks.json
: 构建和编译运行项目,输入Task:Configure Task,模板,Otherslaunch.json
: 调试,读取可执行文件setting.json
: 输入setting
1、配置 tasks.json 编译项目(ctrl+shift+b)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make build",//编译的项目名,build,更改
"type": "shell",
"command": "cd ./build ;cmake ../ ;make",//编译命令,更改
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
2、修改launch.json开始调试
首先要build生成可执行文件,有了可执行文件才能进行debug操作,然后再设置断点,按下F5,进行调试。
点击最左侧第四个小蜘蛛形状的图标(调试),点击左上方的小齿轮,添加配置(C++GDB/LLDB),修改launch.json文件为:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动", // 调试任务名称
"type": "cppdbg", // 任务类型
"request": "launch", // 请求配置类型,launch或者attach
"program": "${workspaceFolder}/build/main", // main为编译后的可执行文件名
"args": [], // 命令行给可执行文件传参数
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build", //工作区间,一定是可执行文件所在的文件夹才能调试
"environment": [],
"externalConsole": false, // 调试时是否显示控制台窗口,一般为true显示控制台
"MIMode": "gdb", // // 指定调试器
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make project", // task.json中的label名
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
3、配置 C++ IntelliSense c_cpp_properties.json
Ctrl+shift+p打开命令选项,选择C/C++:Edit configuration,自动生成 c_cpp_properties.json配置文件
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**", // 当前工作目录
"/usr/local/include/**",
"/usr/include/**" // 系统include引入
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
只要CMakelist.txt配置好了头文件和库文件就不影响编译, c_cpp_properties.json中的includePath主要是在编写代码的时候让vscode找到。
三、windows VSCode ssh远程linux服务器
vscode商店安装Remote SSH,安装之后点击左侧ssh按钮,点击设置按钮
然后在config文件里面编辑链接名称、ip、用户名,保存点击连接按钮选择linux输入密码即可。
参考博客:文章链接