栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

这里主要的难点是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();
}
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 &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;
}

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, double x_s, double y_s,
                   double angle);

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;
}
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)
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)

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

mkdir build && cd build
cmake .. && make 

一个小小的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)
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1037136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号