VScode - ubuntu - C/C++开发环境

安装VS Code

https://code.visualstudio.com/

sudo dpkg -i code_xxxx_amd64.deb

安装插件

vscode-icons 图标插件,资源管理器下各个文件夹的图标
compareit 比较插件,可以用于比较两个文件的差异
C/C++ The C/C++ extension adds language support for C/C++ to Visual Studio Code, including features such as IntelliSense and debugging.
C/C++ Themes UI Themes for C/C++ extension
Better C++ Syntax The bleeding edge of the C++ syntax
C/C++ Snippets C/C++重用代码块
C/C++ Advanced Lint C/C++静态检测
Include AutoComplete 头文件智能补全
Path Intellisense 文件路径智能补全
ARM 支持 ARM 汇编语法高亮显示
DeviceTree 设备树语法插件
C/C++ GNU Global Provide Intellisense for C/C++ using GNU Global
C++ Intellisense C/C++ Intellisense with the help of GNU Global tags
CMake CMake langage support
CMake Tools Extended CMake support
Code Runner 代码运行,仅用于Demo,正式工程应用CMake
Doxygen Documentation Generator 自动生成Doxygen格式注释
Remote - SSH Open any folder on a remote machine using SSH and take advantage of VS Code's full feature set.
Remote - WSL Open any folder in the Windows Subsystem for Linux (WSL) and take advantage of Visual Studio Code's full feature set.
Remote - Containers Open any folder or repository inside a Docker container and take advantage of Visual Studio Code's full feature set.
highlight-words 高亮选中的单词或表达式,需手动设置快捷键,比如设置成“Ctrl + F8”
Bracket Pair Colorizer A customizable extension for colorizing matching brackets
TODO Highlight Highlight TODO, FIXME and other annotations within your code.
Todo Tree Show TODO, FIXME, etc. comment tags in a tree view
Bookmarks Mark lines and jump to them

GNU Global

sudo apt install global

# 在工程目录执行以便建立索引
gtags -v

# 增量更新索引
gtags -vi

# 查找函数或变量的定义和申明
global -x 

# 查找函数或变量的引用
global -xr 

生成三个文,GTAGS是定义的数据库,GRTAGS是引用的数据库,GPATH是路径的数据库

右键即可使用Go to Definition、Peak Definition、Find All References

Lint

sudo apt install clang
sudo apt install cppcheck

违反语言规则或则检查规则,将在违规处显示告警信息,能显著减少编译时间,提高效率。

CMake

sudo apt install cmake

# 创建CMake工程
CMake:Quick Start

# 常用功能
CMake:Configure
CMake:Build
CMake:Clean
CMake:Run
CMake:Debug
CMake:Install

C/C++

如下配置用于研究linux kernel,其他工程也类似

Ctrl + Shift + P >> “C/C++:Edit configurations…(json)”

自动生成“.vscode/c_cpp_properties.json”,在“includePath”中增加头文件路径

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include/**",
                "${workspaceFolder}/include/linux/**",
                "${workspaceFolder}/arch/arm64/**",
                "${workspaceFolder}/arch/arm64/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

Ctrl + Shift + P >> Preferences: Open Workspace Settings (JSON)

自动生成“.vscode/settings.json”

{
    "search.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.DS_Store": true,
        "**/drivers": true,
        "**/sound": true,
        "**/tools": true,
        "**/arch/alpha": true,
        "**/arch/arc": true,
        "**/arch/c6x": true,
        "**/arch/h8300": true,
        "**/arch/hexagon": true,
        "**/arch/ia64": true,
        "**/arch/m32r": true,
        "**/arch/m68k": true,
        "**/arch/microblaze": true,
        "**/arch/mn10300": true,
        "**/arch/nds32": true,
        "**/arch/nios2": true,
        "**/arch/parisc": true,
        "**/arch/powerpc": true,
        "**/arch/s390": true,
        "**/arch/sparc": true,
        "**/arch/score": true,
        "**/arch/sh": true,
        "**/arch/um": true,
        "**/arch/unicore32": true,
        "**/arch/xtensa": true
    },

    // Configure glob patterns for excluding files and folders.
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.DS_Store": true,
        "**/drivers": true,
        "**/sound": true,
        "**/tools": true,
        "**/arch/alpha": true,
        "**/arch/arc": true,
        "**/arch/c6x": true,
        "**/arch/h8300": true,
        "**/arch/hexagon": true,
        "**/arch/ia64": true,
        "**/arch/m32r": true,
        "**/arch/m68k": true,
        "**/arch/microblaze": true,
        "**/arch/mn10300": true,
        "**/arch/nds32": true,
        "**/arch/nios2": true,
        "**/arch/parisc": true,
        "**/arch/powerpc": true,
        "**/arch/s390": true,
        "**/arch/sparc": true,
        "**/arch/score": true,
        "**/arch/sh": true,
        "**/arch/um": true,
        "**/arch/unicore32": true,
        "**/arch/xtensa": true
    },
    "files.associations": {
        "rmap.h": "c",
        "bootmem.h": "c",
        "processor.h": "c",
        "highmem.h": "c",
        "memcontrol.h": "c",
        "page-flags.h": "c"
    }
}

设置垂直标尺

打开"settings",所有“editor.rulers”,点击“settings.json”

image-20210922230743603.png

填入120,表示120个字符。下图右边为垂直标尺。

image-20210922230825154.png

格式化文件

可自定义代码格式,并将名为“.clang-format”的格式文件放到VS Code工程根目录。

sudo apt install clang-format
# 生成llvm风格的格式
clang-format -style="{BasedOnStyle: llvm, IndentWidth: 4}" -dump-config > .clang-format
# 生成google风格的格式
clang-format -style="{BasedOnStyle: google, IndentWidth: 4}" -dump-config > .clang-format

自定义格式,参考 https://zhuanlan.zhihu.com/p/356143396

快捷键:ctrl + shift + I

或则右键并选择Format Document

重命名

选择需重名的Symbol,右键并选择“Rename Symbol”,输入New Symbol

按“Shift+Enter”,然后选择需要修改的代码,并按“Shift+Enter“

image-20211010160827118.png

Git

Git History View git log, file history, compare branches or commits
GitLens — Git supercharged Supercharge the Git capabilities built into Visual Studio Code — Visualize code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more
Git Graph View a Git Graph of your repository, and perform Git actions from the graph.

在“Source Control”进行操作

Remote - SSH

配置和使用请参考:

https://code.visualstudio.com/docs/remote/ssh#_getting-started

公司的办公环境一般是window,开发环境为ubuntu。通过此插件连接到ubuntu进行开发,大部分功能是在ubuntu上执行,PC仅显示和编辑代码。

备注: 连接到Remote Server,须选择本地的插件安装到Remote server

代理

由于数据安全,大公司会屏蔽部分外网功能并提供代理服务。

打开“settings”,搜索“proxy”,在“Http:Proxy”中输入代理网址

image-20210922225826558.png

参考

  • https://code.visualstudio.com/docs
  • https://blog.csdn.net/p1279030826/article/details/105340991
  • https://my.oschina.net/u/4256916/blog/3311800
  • https://www.bilibili.com/video/BV1az411B7hd?from=search&seid=14050932776868743483&spm_id_from=333.337.0.0
  • https://blog.csdn.net/qq_43406338/article/details/109397831

你可能感兴趣的:(VScode - ubuntu - C/C++开发环境)