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; }
CMakeLists.txt
add_executable(glog_test src/Test/GlogTest.cpp)
target_link_libraries( glog_test
${catkin_LIBRARIES}
glog
)
不需要添加find_package和include_directories
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(" usage "); 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; }
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 )
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 << " "; return 0; }
CMakeLists.txt
find_package(absl REQUIRED) add_executable(abseil_test src/Test/Abseil.cpp) target_link_libraries( abseil_test ${catkin_LIBRARIES} absl::strings )
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; }
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)
内层目录
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"; }
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
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)