google相关的基础软件

1、glog

GlogTest.cpp

复制代码
#include <iostream>
#include <stdlib.h>
#include <glog/logging.h>
#include <ros/ros.h>
int main(int argc, char** argv)
{
    google::InitGoogleLogging(argv[0]);
    google::SetLogDestination(google::GLOG_INFO, "./test_glog_info");

    ros::init(argc, argv, "glog_node");
    ros::NodeHandle nh("glog");
    LOG(INFO) << "Hello glog";
    return 0;
}
View Code
复制代码

CMakeLists.txt

复制代码
add_executable(glog_test src/Test/GlogTest.cpp)
target_link_libraries( glog_test
        ${catkin_LIBRARIES}
        glog
)

不需要添加find_package和include_directories
View Code
复制代码

2、gflags

GflagsTest.cpp

复制代码
//
// Created by gary on 2021/1/16.
//

#include <iostream>

#include <gflags/gflags.h>
#include <ros/ros.h>

DEFINE_string(host, "127.0.0.1", "the sever host");
DEFINE_int32(port, 12306, "the sever port");

int main(int argc, char ** argv)
{
    google::SetUsageMessage("\n usage\n");
    google::ParseCommandLineFlags(&argc, &argv, true);
    ros::init(argc, argv, "gflags_node");
    ros::NodeHandle nh("gflags");
    std::cout << "The sever host is: " << FLAGS_host
    << ", the server port is: " << FLAGS_port << std::endl;
    return 0;
}
View Code
复制代码

CMakeLists.txt

复制代码
add_compile_options(-std=c++11)
add_compile_options(-pthread)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

find_package(gflags REQUIRED)
include_directories(${gflags_INCLUDE_DIRS})
add_executable(gflag_test src/Test/GflagsTest.cpp)
target_link_libraries( gflag_test
        ${catkin_LIBRARIES}
        gflags
        )
View Code
复制代码

3、Abseil

Abseil.cpp

复制代码
#include <iostream>
#include <string>
#include <vector>
#include "absl/strings/str_join.h"
#include <ros/ros.h>
int main(int argc, char** argv) {

    ros::init(argc, argv, "gtest_node");
    ros::NodeHandle nh("gtest");
    std::vector<std::string> v = {"foo","bar","baz"};
    std::string s = absl::StrJoin(v, "-");

    std::cout << "Joined string: " << s << "\n";
    return 0;
}
View Code
复制代码

CMakeLists.txt

复制代码
find_package(absl REQUIRED)
add_executable(abseil_test src/Test/Abseil.cpp)
target_link_libraries( abseil_test
        ${catkin_LIBRARIES}
        absl::strings
        )
View Code
复制代码

4、Gtest

外层

GTestMain.cpp

复制代码
#include <iostream>
#include <ros/ros.h>

int main(int argc, char** argv)
{
    ros::init(argc, argv, "gtest_node");
    ros::NodeHandle nh("gtest");
    std::cout << "in main " << std::endl;
    return 0;
}
View Code
复制代码

CMakeLists.txt

复制代码
include_directories({PROJECT_SOURCE_DIR}/src)
enable_testing()
set(GTest_project src/Test/GTest/Test/MyClass.hpp)
add_executable(gtest_ ${GTest_project}
                     src/Test/GTest/GTestMain.cpp)
target_link_libraries( gtest_
        ${catkin_LIBRARIES}
        )

add_subdirectory(src/Test/GTest/Test)
View Code
复制代码

内层目录

GTest/Test

下面的内容

MyClass.cpp

复制代码
#include <gtest/gtest.h>

#include "MyClass.hpp"

TEST(test_my_class, get_age)
{
    my_class myClass("Joel", 21);
    ASSERT_TRUE(myClass.get_age() == 21) << "age is not 16";
}

TEST(test_my_class, get_name)
{
    my_class myClass("Joel", 21);
    ASSERT_EQ(myClass.get_name(), "Joel") << "name is not Joel";
}
View Code
复制代码

MyClass.hpp

复制代码
#ifndef SRC_MYCLASS_HPP
#define SRC_MYCLASS_HPP
#pragma once

#include <string>

class my_class
{
public:
    my_class(const std::string& name, int age)
    {
        m_age = age;
        m_name = name;
    }

public:
    int get_age() { return m_age; }
    std::string get_name() { return m_name; }

private:
    int m_age;
    std::string m_name;
};
#endif //SRC_MYCLASS_HPP
View Code
复制代码

CMakeLists.txt

复制代码
# 查找 GTest 库
find_package(GTest REQUIRED)
# GTest 的头文件
include_directories(${GTEST_INCLUDE_DIRS})

add_executable(test_my_class MyClass.cpp)

# 链接测试库
target_link_libraries( test_my_class
        ${GTEST_BOTH_LIBRARIES}
        pthread)

# 添加到测试
gtest_discover_tests(test_my_class)
View Code
复制代码

 

posted on   gary_123  阅读(202)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2017-01-16 第四课4、ROS客户端
2017-01-16 第三课3、ROS的launch文件
2016-01-16 Monte carlo

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示