前言
搭建一套持续集成系统,覆盖代码格式规范、静态检查、代码覆盖率、ut环境、内存泄漏检测等,通过vscode集成开发。
搭建环境
- 拉取基础镜像,启动container
# pull docker image and run.
$ docker pull yagerfgcs/rtms:v0.1
$ cd {work_dir} # cd to workdir
$ docker run --name rtms_dbg --privileged --net=host -v `pwd`:/tmp/rtms -it yagerfgcs/rtms:v0.1 /bin/bash
- 如果在本机mac上启动docker,需要在本机启动ssh server
方法:打开mac设置->共享->远程管理,勾选“远程登录”
- 通过vs-code增加SSH Targets
方法:打开vs-code->remote explorer->Configure->选择“{$user}/.ssh/config”文件->添加ssh host->“Connect to Host in Current Window”
- 通过vs-code连接到docker容器
方法:将remote explorer切换到“Containers”,选择已启动的容器
ps:如果登录宿主机的账号无root权限,导致以上页面无法加载containers,解决办法执行以下命令
# Create the docker group.
sudo groupadd docker
# Add your user to the docker group.
sudo usermod -aG docker $USER
# **activate the changes to groups
newgrp docker
参考链接:https://docs.docker.com/engine/install/linux-postinstall/
使用vscode调试工程的配置方法
在Mac端远程调试Linux服务器的工程的方法
- 首先使用ssh配置连接远程服务器,方法参考上面的“通过vs-code增加SSH Targets”
- cmake编译的项目参考:
- CMake: Debug and launch
- 主要是.vscode下的launch.json的配置
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
// add the directory where our target was built to the PATHs
// it gets resolved by CMake Tools:
"name": "PATH",
"value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
},
{
"name": "OTHER_VALUE",
"value": "Something something"
}
],
"console": "externalTerminal",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
- 注意需配置kits

代码规范:CLang-Format
- 首先安装Clang-format插件,推荐链接
- 配置远程container的设置项
- 配置clang-format executable path,安装插件后,可以查找具体的clang-format可执行文件目录
$ find / -name *clang-format*
/root/.vscode-server/extensions/ms-vscode.cpptools-1.13.2-linux-x64/LLVM/bin/clang-format
/root/.vscode-server/extensions/xaver.clang-format-1.9.0
也可以直接修改settings.json文件
{
"clang-format.executable": "/root/.vscode-server/extensions/ms-vscode.cpptools-1.13.2-linux-x64/LLVM/bin/clang-format",
"clang-format.fallbackStyle": "LLVM",
"C_Cpp.clang_format_path": "${workspace}/.clang-format",
}
ps:如果不想使用插件的clang-format,可以升级yum源再单独安装,方法参考:https://blog.youkuaiyun.com/shenmingxueIT/article/details/122125191
-
将通用format文件放到vs的workspace目录下
通用的.clang-format模板,可通过命令生成。
clang-format -style=Google -dump-config > .clang-format -
具体静态扫描的方法:
在具体文件,右键–>“Format Document”
静态代码检查:Cpplint
-
背景
使用google代码规范时可以使用cpplint脚本检查。 -
方法:
- 下载cpplint.py,地址:https://github.com/google/styleguide/blob/gh-pages/cpplint/cpplint.py放到工程目录下。
- 安装python工具。
- 配置CPPLINT.cfg文件。
-
执行方式
# 检查具体文件
python cpplint.py ./test.cpp
# 检查整个目录
python cpplint.py ./*.cpp
静态代码扫描:CppCheck
- 介绍:https://cppcheck.sourceforge.io/
- 安装cppcheck:参考官网;
- 配置cppcheck的bin目录到系统$PATH目录
[root@docker-desktop ~]# echo $PATH
/root/.vscode-server/bin/64bbfbf67ada9953918d72e1df2f4d8e537d340e/bin/remote-cli:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/root/bin:/root/bin:/tmp/cppcheck-2.9/bin
- 在vscode中安装cppcheck相关插件,推荐 Cppcheck Plug-in
- 配置clang.executable
or直接在setting.json文件中增加
"clang.executable": "/opt/rh/llvm-toolset-7/root/usr/bin/clang"