• ubuntu下vscode+cmake实现gtest(cmake引入gtest,glog)


    0 开始之前你要准备的内容有:

    1. Vscode(扩展:C/C++,C/C++ Extension Pack,CMake,CMake Tools)
    2. ubuntu 18.04 or 22.04 (author)
    3. libgtest,libglog安装教程请自行查找

    1 整体代码框架如下:

    test_demo
    ├── CMakeLists.txt
    ├── gtest
    │   ├── CMakeLists.txt
    │   └── gtest.cpp
    ├── include
    │   └── 2dtf
    │       ├── 2dtf.cpp
    │       └── 2dtf.h
    └── src
        └── main.cpp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    编译后的代码在这,请删除build中的所有文件后使用。
    这里主要的难点是cmakelists.txt的书写

    2. gtest.cpp
    #include "../include/2dtf/2dtf.h"
    // #include 
    #include 
    
    My_Math m;
    TEST(max_num1, max_num) {
      double x = 1.0, y = 2.3;
      double m1 = x;
      if (x <= y) {
        m1 = y;
      }
      //实现glog时需要添加参数,这里没有添加所以先去掉了,没有参与测试20220809/14:33
      //  google::InitGoogleLogging("");
      // LOG(INFO) << "start testing!";
      EXPECT_EQ(2.3, m.max_num(x, y));
    
      // google::ShutdownGoogleLogging();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    3. 2dtf.cpp
    #include "2dtf.h"
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    double My_Math::max_num(double a, double b) { return a > b ? a : b; }
    
    int My_Math::plus_num(int a, int b) { return a + b; }
    
    int My_Math::muti_num(int a, int b) { return a * b; }
    
    double Transfor_2D(std::vector<Point> &points, double x_s, double y_s,
                       double angle) {
      transform(begin(points), end(points), begin(points), [=](const Point &point) {
        double sin_val = sin(angle);
        double cos_val = std::cos(angle);
        Point trans_point((point.x - x_s) * cos_val + (point.y - y_s) * sin_val,
                          (point.y - y_s) * cos_val - (point.x - x_s) * sin_val);
        return trans_point;
      });
    
      return 0.0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    4. 2dtf.h
    #include 
    #include 
    #include 
    #include 
    #include 
    
    class My_Math {
    
    public:
      //实现基本加减乘除的内容
      double max_num(double a, double b);
      int plus_num(int a, int b);
      int muti_num(int a, int b);
    };
    
    struct Point {
      explicit Point() : x(0.0), y(0.0) {}
      explicit Point(double x_i, double y_i) : x(x_i), y(y_i) {}
      double x;
      double y;
    };
    
    double Transfor_2D(std::vector<Point> &point, double x_s, double y_s,
                       double angle);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    5. main.cpp
    #include 
    #include 
    
    #include "../include/2dtf/2dtf.h"
    
    using namespace std;
    int main() {
      My_Math mm;
      double x = 1.4, y = 4.55;
      int a = 2, b = 10;
    
      cout << "namespace testing!" << endl;
      cout << "max_num is: " << mm.max_num(x, y)
           << "plus_num is: " << mm.plus_num(a, b) << endl;
    
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    6. 最外层的CMakeLists.txt
    #最小的要求版本
    cmake_minimum_required(VERSION 3.1.0)
    #更换目前的项目的名称,后台生成一个文件路径${PROJECT_SOURCE_DIR},cmake所在的文件夹以及${PROJECT_NAME}表示当前的项目名称,这里相当于MyProject
    project(MyProject)
    #设置c++标准为11
    set(CMAKE_CXX_STANDARD 11)
    #包含头文件地址,本文文件路径还有另一种写法是include_directories(./include)
    include_directories(${PROJECT_SOURCE_DIR}/include)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
    #给头文件换名字,以后就可以使用${修改后的名字的大写}代替程序进行编译链接等工作
    set(MY_PROJECT_SRC
            include/2dtf/2dtf.h)
    #与上面功能相同
    set(2DTF_SRCS include/2dtf/2dtf.cpp)  
    #添加可执行文件   
    add_executable(MyProject src/main.cpp ${2DTF_SRCS})
    
    
    # 开启测试
    enable_testing()
    # 添加测试目录
    add_subdirectory(gtest)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    7. gtest目录下的CMakeLists.txt
    cmake_minimum_required(VERSION 3.10)
    project(gtest VERSION 1.0)
    # 查找 GTest 库
    find_package(GTest REQUIRED)
    #find_package(Glog REQUIRED)
    # GTest 的头文件
    include_directories(./include)
    set(2DTF_SRCS ../include/2dtf/2dtf.cpp)
    if(GTEST_FOUND)
        message("GTest library was found! ")
    else(GTEST_FOUND)
        message("GTest library is needed but can't be found! ")
    endif()
    # if(Glog_FOUND)
    #     message("GLog library was found! ")
    # else(Glog_FOUND)
    #     message("GLog library is needed but can't be found! ")
    # endif()
     #包含gtest头文件   
    include_directories(${GTEST_INCLUDE_DIRS})
    include_directories(${GLOG_INCLUDE_DIRS})
    
    add_executable(gtest gtest.cpp ${2DTF_SRCS})
    # 链接测试库
    target_link_libraries(gtest ${GTEST_BOTH_LIBRARIES} pthread )
    
    # 添加到测试,这里会添加到主cmake中
    gtest_discover_tests(gtest)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    上面的程序写完后,在文件夹test_demo中使用以下命令创建build文件夹以及编译所有文件,最后会在build文件夹生成名为MyProject的可执行文件,在build/gtest文件下生成名为gtest可执行文件。执行的结果如下图:

    mkdir build && cd build
    cmake .. && make 
    
    • 1
    • 2

    在这里插入图片描述

    一个小小的CMakeLists.txt模板
    #首先确定最小版本号
    cmake_minimum_required(VERSION 3.10)
    #给目前写的工程定义一个名字,后续编译时使用这个名字
    #更换目前的项目的名称,后台生成一个文件路径${PROJECT_SOURCE_DIR},cmake所在的文件夹以及${PROJECT_NAME}表示当前的项目名称
    project(gtest VERSION 1.0)
    # 查找 GTest 库
    find_package(GTest REQUIRED)
    #find_package(Glog REQUIRED)
    
    #包含使用到的头文件地址,多个地址的时候直接往后添加就可以
    include_directories(./include ./best ./better)
    #不会写了就直接set,先设置所有源文件,类中的cpp,最后直接放在引用的cpp后面生成可执行文件就可以了
    set(2DTF_SRCS ../include/2dtf/2dtf.cpp)
    
    #这里可以输出一部分message,查看是否找到库文件啦,是否设置某些东西啦
    if(GTEST_FOUND)
        message("GTest library was found! ")
    else(GTEST_FOUND)
        message("GTest library is needed but can't be found! ")
    endif()
    # if(Glog_FOUND)
    #     message("GLog library was found! ")
    # else(Glog_FOUND)
    #     message("GLog library is needed but can't be found! ")
    # endif()
     #包含库文件头文件   
    include_directories(${GTEST_INCLUDE_DIRS})
    include_directories(${GLOG_INCLUDE_DIRS})
    
    #生成可执行文件
    add_executable(gtest gtest.cpp ${2DTF_SRCS})
    # 链接测试库
    target_link_libraries(gtest ${GTEST_BOTH_LIBRARIES} pthread )
    
    # 单文件夹下的cmnakelists.txt添加到测试
    gtest_discover_tests(gtest)
    #主文件下的cmnakelists.txt添加
    add_subdirectory(gtest)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    angle_ll
    数据结构: 二叉搜索树
    Java--SpringMVC之处理器方法返回值
    docker 安装 RabbitMq
    如何防止IP和账户关联?
    Mysql之数据处理及数据汇总函数
    什么是Ribbon,怎么实现负载均衡?
    栈和队列概念
    [CISCN2019 华北赛区 Day1 Web2]ikun-1|python反序列化
    浅谈:在ZK-rollup和以太坊上的帐户抽象
  • 原文地址:https://blog.csdn.net/NotANumber123/article/details/126246983