ubuntu 搭建 cmake + vscode 的 c/c++ 开发环境

todo 列表

软件安装

基本的环境搭建

最基本的 vscode 插件

只需要安装如下两个插件即可

c/c++ 扩展是为了最基本的代码提示和调试支持

cmake language support 是为了提示 CMakeLists.txt 脚本

image

image

有可能安装了 cmake language support 还是没有代码提示, 注意配置 cmake 路径

image

代码

main.cpp

#include <stdio.h>

int main()
{
    printf("\nhello world\n\n");
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.24)

project(hello_ubuntu CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_executable(${PROJECT_NAME} main.cpp)

任务配置

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build-debug",
            "type": "shell",
            "command": "cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug && cmake --build cmake-build-debug",
            "dependsOn": [
                "configure"
            ]
        },
        {
            "label": "build-release",
            "type": "shell",
            "command": "cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release && cmake --build cmake-build-release",
            "dependsOn": [
                "configure"
            ]
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "rm -rf build && rm -rf cmake-build-debug && rm -rf cmake-build-release"
        },
        {
            "label": "rebuild",
            "type": "shell",
            "dependsOn": [
                "clean",
                "build-debug",
                "build-release"
            ]
        },
        {
            "label": "run",
            "type": "shell",
            "command": "./cmake-build-release/hello_ubuntu",
            "dependsOn": [
                "build-release"
            ]
        }
    ]
}

此时可以通过终端菜单的运行任务来运行

改进任务的运行方式

安装如下插件
image

Task Buttons 插件

.vscode文件夹添加.settings.json,并添加如下内容

{
    "VsCodeTaskButtons.showCounter": true,
    "VsCodeTaskButtons.tasks": [
        {
            "label": "$(notebook-delete-cell) clean",
            "task": "clean"
        },
        {
            "label": "$(debug-configure) rebuild",
            "task": "rebuild"
        },
        {
            "label": "$(notebook-execute) run",
            "task": "run"
        }
    ]
}

然后状态栏就会出现对应的按钮, 直接点击任务对应的按钮即可运行任务. 图标从 这里 获取

image

Task Explorer 插件

此插件将提供了一个任务面板, 安装之后 查看->打开试图 搜索Task Explorer 即可打开此面板, 拖到自己喜欢的位置然后直接点击对应任务右侧的按钮即可运行任务. 任务太多的话, 可以将任务加入 Favorites 列表, 把其他的收起来就可以了
image

快捷键

参考: https://blog.csdn.net/qq_45859188/article/details/124529266

debug

参考 这里, 直接在 .vscode 文件夹下添加 launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "test-debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/cmake-build-debug/hello_ubuntu",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "/usr/bin/gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "rebuild"
    }
  ]
}

打一个断点, 然后直接 F5

image

注意: 有时候 vscode 的 debug 会出问题, 此时直接执行 clean 任务再进行调试即可

clang-format

参考资料

https://clang.llvm.org/docs/ClangFormat.html
中文版: https://www.cnblogs.com/PaulpauL/p/5929753.html

clang-format的介绍和使用

安装

sudo apt-get install clang-format
laolang@laolang-pc:~$ clang-format --version
Ubuntu clang-format version 14.0.0-1ubuntu1
laolang@laolang-pc:~$ 

配置

# 导出 Google 风格
clang-format --style=Google -dump-config > .clang-format

一个最简单的配置文件如下, 然后根据上一步导出的文件再进行细节调整

---
Language:        Cpp
BasedOnStyle:  Google

使用

使用给定的配置:.clang-format 格式化文件: main.cpp

clang-format -style=file:.clang-format -i main.cpp

格式化 src 目录

#!/bin/bash
find src -type f \( -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -exec clang-format -style=file:.clang-format -i {} \;

C语言 整合

CUnit

参考资料

官网 : https://cunit.sourceforge.net/

github: https://github.com/jacklicn/CUnit

官方手册: https://cunit.sourceforge.net/doc/index.html

中文手册: 【单元测试】CUnit用户手册(中文)

简明教程: 【单元测试】CUnit单元测试框架(不支持mock功能)

ubuntu下安装CUnit出现的问题及解决

安装

sudo apt-get update
sudo apt-get install build-essential automake autoconf libtool
mv configure.in configure.ac
aclocal
autoconf 
autoheader 
libtoolize --automake --copy --debug --force
automake --add-missing
automake
./configure 
make
sudo make install

测试

#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>
#include <CUnit/Automated.h>

/* 被测试的函数,在当中故意安装了一个BUG */
static int sum(int a, int b)
{
    if (a > 4)
    {
        return 0;
    }
    return (a + b);
}

static int suite_init(void)
{
    return 0;
}

static int suite_clean(void)
{
    return 0;
}

static void test_sum(void)
{
    CU_ASSERT_EQUAL(sum(1, 2), 3);
    CU_ASSERT_EQUAL(sum(5, 2), 7);
}

int main()
{
    CU_pSuite pSuite = NULL;

    /* initialize the CUnit test registry */
    if (CUE_SUCCESS != CU_initialize_registry())
    {
        return CU_get_error();
    }

    /* add a suite to the registry */
    pSuite = CU_add_suite("suite_sum", suite_init, suite_clean);
    if (NULL == pSuite)
    {
        CU_cleanup_registry();
        return CU_get_error();
    }

    /* add the tests to the suite */
    if ((NULL == CU_add_test(pSuite, "test_sum", test_sum)))
    {
        CU_cleanup_registry();
        return CU_get_error();
    }

    // basic
    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();

    // automated
    CU_list_tests_to_file();
    CU_automated_run_tests();

    /* Clean up registry and return */
    CU_cleanup_registry();
    return CU_get_error();
}

编译

gcc test.c `pkg-config --libs --cflags cunit` -o test

此时控制台有了 basic 模式的输出, 并且有了 automated 模式的 xml 文件

laolang@laolang-pc:~/tmp/cunit$ ./test 


     CUnit - A unit testing framework for C - Version 2.1-3
     http://cunit.sourceforge.net/


Suite: suite_sum
  Test: test_sum ...FAILED
    1. test.c:33  - CU_ASSERT_EQUAL(sum(5, 2),7)

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      1      1      0      1        0
             asserts      2      2      1      1      n/a

Elapsed time =    0.000 seconds
laolang@laolang-pc:~/tmp/cunit$ l
总计 32K
-rw-rw-r-- 1 laolang laolang 1.7K 2023-06-11 18:17:16 CUnitAutomated-Listing.xml
-rw-rw-r-- 1 laolang laolang 1.6K 2023-06-11 18:17:16 CUnitAutomated-Results.xml
-rwxrwxr-x 1 laolang laolang  17K 2023-06-11 18:17:14 test*
-rw-rw-r-- 1 laolang laolang 1.2K 2023-06-11 18:17:02 test.c
laolang@laolang-pc:~/tmp/cunit$ 

然后从安装包复制如下几个文件, 和 cunit 输出的 xml 同级

  • CUnit-List.dtd
  • CUnit-List.xsl
  • CUnit-Run.dtd
  • CUnit-Run.xsl

在本地起一个服务器, 比如 npm 的 serve, 两个文件效果如下

image

image

关于代码覆盖率

参考: GCOV+LCOV 代码调试和覆盖率统计工具

日志

日志框架有很多, 此处选择 zlog, 官网写的非常详细

github: https://github.com/HardySimpson/zlog/

中文手册: http://hardysimpson.github.io/zlog/UsersGuide-CN.html

整合结果

目录结构

laolang@laolang-pc:~/tmp/helloc$ tree -a
.
├── app.log
├── CMakeLists.txt
├── coverage.sh
├── .gitignore
├── resources
│   └── cunit
│       ├── CUnit-List.dtd
│       ├── CUnit-List.xsl
│       ├── CUnit-Run.dtd
│       └── CUnit-Run.xsl
├── src
│   ├── app
│   │   ├── CMakeLists.txt
│   │   └── helloc.c
│   ├── CMakeLists.txt
│   ├── common
│   │   ├── CMakeLists.txt
│   │   ├── common.h
│   │   ├── zlog_conf.c
│   │   └── zlog_conf.h
│   └── datastruct
│       ├── CMakeLists.txt
│       ├── sum.c
│       └── sum.h
├── test
│   ├── CMakeLists.txt
│   ├── maintest.c
│   ├── test_sum.c
│   └── test_sum.h
├── .vscode
│   ├── launch.json
│   ├── settings.json
│   └── tasks.json
└── zlog.conf

8 directories, 26 files
laolang@laolang-pc:~/tmp/helloc$ 

.vscode

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build-debug",
            "type": "shell",
            "command": "cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug && cmake --build cmake-build-debug",
            "dependsOn": [
                "configure"
            ]
        },
        {
            "label": "build-release",
            "type": "shell",
            "command": "cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release && cmake --build cmake-build-release",
            "dependsOn": [
                "configure"
            ]
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "rm -rf build && rm -rf cmake-build-debug && rm -rf cmake-build-release"
        },
        {
            "label": "rebuild",
            "type": "shell",
            "dependsOn": [
                "clean",
                "build-debug",
                "build-release"
            ]
        },
        {
            "label": "run",
            "type": "shell",
            "command": "./cmake-build-release/bin/helloc",
            "dependsOn": [
                "build-release"
            ]
        },
        {
            "label": "test",
            "type": "shell",
            "command": "./cmake-build-debug/bin/helloc_test && mkdir -p cmake-build-debug/report && mv CUnit*.xml cmake-build-debug/report && cp resources/cunit/CUnit-*.* cmake-build-debug/report/",
            "dependsOn": [
                "build-debug"
            ]
        },
        {
            "label": "coverage",
            "type": "shell",
            "command": "./coverage.sh",
            "dependsOn": [
                "clean",
                "test"
            ]
        }
    ]
}

settings.json

{
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/Thumbs.db": true,

        "**/cmake-build-debug":true,
        "**/cmake-build-release":true
    },
    "cmake.cmakePath": "/home/laolang/program/cmake/bin/cmake",
    "VsCodeTaskButtons.showCounter": true,
    "VsCodeTaskButtons.tasks": [
        {
            "label": "$(notebook-delete-cell) clean",
            "task": "clean"
        },
        {
            "label": "$(debug-configure) rebuild",
            "task": "rebuild"
        },
        {
            "label": "$(notebook-execute) run",
            "task": "run"
        },
        {
            "label": "$(test-view-icon) test",
            "task": "test"
        },
        {
            "label": "coverage",
            "task": "coverage"
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "app-debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/cmake-build-debug/bin/helloc",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "rebuild"
        }
    ]
}

coverage.sh

#!/bin/bash
BUILD_PATH=cmake-build-debug
lcov -d . -o ${BUILD_PATH}/app.info -b . -c --exclude '*/test/*' --exclude '*/src/main/*'
genhtml ${BUILD_PATH}/app.info -o ${BUILD_PATH}/lcov

zlog.con

[formats]

simple = "%d().%ms %p %V [%F:%L] - %m%n"

[rules]

my_cat.DEBUG    >stdout;    simple
*.*     "app.log", 10MB * 0 ~ "app-%d(%Y%m%d).#2s.log"

cmake

顶层 cmake

cmake_minimum_required(VERSION 3.0)

project(helloc C)

set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED True)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH True)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage -Wall")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
endif()

set(lib_common common)
set(lib_datastruct datastruct)


configure_file(${CMAKE_SOURCE_DIR}/zlog.conf ${CMAKE_BINARY_DIR}/bin/zlog.conf COPYONLY)

add_subdirectory(src)

add_subdirectory(test)
enable_testing()
add_test(NAME helloc_test COMMAND helloc_test)

test cmake

cmake_minimum_required(VERSION 3.25)

project(helloc_test C)

set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED True)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH True)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage -Wall")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")

include_directories(${CMAKE_SOURCE_DIR}/test)
include_directories(${CMAKE_SOURCE_DIR}/include)

aux_source_directory(. TEST_SRCS)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable(${PROJECT_NAME} ${TEST_SRCS})
target_link_libraries(${PROJECT_NAME} cunit zlog ${lib_common} ${lib_datastruct})
set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH "\${ORIGIN}/../lib")

其他文件与脚本

效果预览

cunit 的测试报告上面已经有了, 代码覆盖率如下
image
image
image

注意事项

  1. 代码覆盖率要求代码必须运行过
  2. 如果生成代码覆盖率或者运行测试的时候 lcov 报错, 有可能是因为覆盖率数据文件冲突, 先执行 clean 再执行 test 或者 coverage 即可
  3. vscode 的 debug 有可能会崩溃, 结束任务, 关闭终端面板, 手动删除 build 目录再次点击 F5 即可
  4. 尝试在 tasks.json 中配置变量, 失败了
posted @ 2023-06-11 19:17  laolang2016  阅读(451)  评论(0编辑  收藏  举报