首页 > 系统相关 >ubuntu 搭建 cmake + vscode 的 c/c++ 开发环境

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

时间:2023-06-11 19:11:33浏览次数:56  
标签:cmake vscode c++ label debug build test CMAKE

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 任务再进行调试即可

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")

其他的配置与代码没有什么特别的

标签:cmake,vscode,c++,label,debug,build,test,CMAKE
From: https://www.cnblogs.com/khlbat/p/17454015.html

相关文章

  • C++面试题
    1、当使用C++编写代码时,有一个常见的问题是如何在子类中调用父类的构造函数。下面是一个相关的C++面试题:题目:假设有一个基类Animal,其中包含一个带参数的构造函数和一个公共成员函数display()。请编写一个派生类Dog,继承自Animal,并实现自己的构造函数和display()函数。要求:Dog......
  • C/C++数学口算比赛系统[2023-06-11]
    C/C++数学口算比赛系统[2023-06-11]题目三数学口算比赛系统设计要求:适用于小学生数学口算比赛的系统。比赛题型分为两种:“四则简单运算”和“四则混合运算”,计算机随机出题,选手计时回答。要求进入每种题型比赛时,计算机均有提示,每人的得分情况随时更新。菜单格式如图。基......
  • c++面试学习2
    1.排序算法及其比较次数排序次数的数量级决定了排序算法的复杂度(作为个人纪录,下面图片写的不要清晰见谅) 2.fgets(s,n,f)函数的功能:原型是char*fgets(char*s,intn,FILE*stream);从流中读取n-1个字符,除非读完一行,s用来接收字符串,如果读取成功返回s的指针,否则返回NUL......
  • c++单件模式
    1.意图      保证一个类仅有一个实例,并提供一个访问它的全局访问点。2.动机      对一些类来说,只有一个实例是很重要的。虽然系统中可以有许多打印机,但却只应该有一个打印假脱机(printerspooler),只应该有一个文件系统和一个窗口管理器。一个数字滤波......
  • 玩转Google开源C++单元测试框架Google Test系列(gtest)(总)
    前段时间学习和了解了下Google的开源C++单元测试框架GoogleTest,简称gtest,非常的不错。我们原来使用的是自己实现的一套单元测试框架,在使用过程中,发现越来越多使用不便之处,而这样不便之处,gtest恰恰很好的解决了。其实gtest本身的实现并不复杂,我们完全可以模仿gtest,不断的完善我们......
  • 用C++封装的ADO类
    用C++封装的ADO类作者:刘振海.H文件//ADO.h:interfacefortheCADOclass.////#if!defined(AFX_ADO_H__5A466E67_5E04_445D_9CB0_C64650B9AC68__INCLUDED_)#defineAFX_ADO_H__5A466E67_5E04_445D_9CB0_C64650B9AC68__INCLUDED_#if_MSC_VER>1000#pragmaonce......
  • 详解VOLATILE在C++中的作用
    VOLATILE的介绍     volatile类似于大家所熟知的const也是一个类型修饰符。volatile是给编译器的指示来说明对它所修饰的对象不应该执行优化。volatile的作用就是用来进行多线程编程。在单线程中那就是只能起到限制编译器优化的作用。所以单线程的童鞋们就不用浪费精力看下......
  • [c/c++/OC]高质量的面试题及答案及注解
    一、选择题C语言:1.声明语句为inta[3][4];下列表达式中与数组元素a[2][1]等价的是(A)。A、*(a[2]+1)B、a[9]C、*(a[1]+2)D、*(*(a+2))+1a[2]<==>*(a+2)是等价的C两个数反过来了,D、1放进去2.请问经过表达式a=5?0:1的运算,变量a的最终值是(C......
  • C++面试八股文:struct、class和union有哪些区别?
    C++面试八股文:struct、class和union有哪些区别?某日小二参加XXX科技公司的C++工程师开发岗位5面:面试官:struct和class有什么区别?小二:在C++中,struct和class的唯一区别是默认的访问控制。struct默认的成员是public的,而class的默认成员是private的。面试官:struct、class和unio......
  • [C/C++] 结构体
    在C语言中,字符串实际上是使用空字符\0结尾的一维字符数组。因此,\0是用于标记字符串的结束。固定缓冲区#defineMAX_LEN100#defineDATA_LEN100typedefstruct{intlength;chardata[MAX_LEN];//固定缓冲区大小,浪费内存}max_buffer,*max_buffer_ptr;......