ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to build gtest tests in ros2?

asked 2021-03-29 16:00:58 -0500

lexi gravatar image

updated 2021-03-29 18:30:56 -0500

jayess gravatar image

I am trying to build tests with ros2, but I'm getting linker errors with gtest.

I guess my question is: "Why am I getting linking errors with gtest?"

This is my CMake:

cmake_minimum_required(VERSION 3.5)
project(bluebotics_ant)

find_package(ament_cmake REQUIRED)
find_package(ethernetip_interface REQUIRED)
find_package(cpp_threading_helpers REQUIRED)
find_package(rclcpp REQUIRED)
find_package(ament_index_cpp REQUIRED)
find_package(ament_cmake_gtest REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(std_msgs REQUIRED)

find_package(Boost 1.47 REQUIRED COMPONENTS system)

rosidl_generate_interfaces(${PROJECT_NAME}
        # messages
        "msg/ANTInfo.msg"
        # actions
        # "action/example.action"
        DEPENDENCIES builtin_interfaces)

set(${PROJECT_NAME}_SRCS
        src/ant_node.cpp
        src/ant_driver.cpp
        src/ant_generic_data_assembly.cpp
        src/plc_generic_data_assembly.cpp
        test/assembly_time_values_test.cpp
        src/ant_kinematics_assembly.cpp)

add_executable(ant_test src/main.cpp ${${PROJECT_NAME}_SRCS})
target_include_directories(ant_test
        PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
        )
ament_target_dependencies(
        ant_test
        rclcpp
        ethernetip_interface
        cpp_threading_helpers
        ament_index_cpp
)
rosidl_target_interfaces(ant_test
        ${PROJECT_NAME} "rosidl_typesupport_cpp")

install(TARGETS
        ant_test
        DESTINATION lib/${PROJECT_NAME}
        )

# Install EtherNet/IP ANT device config as symlink
INSTALL(CODE "execute_process( \
    COMMAND ${CMAKE_COMMAND} -E create_symlink \
    ${CMAKE_SOURCE_DIR}/config/data.yaml \
    ${CMAKE_INSTALL_PREFIX}/share/data.yaml \
    )"
        )
# Install EtherNet/IP ANT device kinematics config as symlink
INSTALL(CODE "execute_process( \
    COMMAND ${CMAKE_COMMAND} -E create_symlink \
    ${CMAKE_SOURCE_DIR}/config/tricycle_kinematics.yaml \
    ${CMAKE_INSTALL_PREFIX}/share/tricycle_kinematics.yaml \
    )"
        )

if (BUILD_TESTING)
    find_package(ament_lint_auto REQUIRED)
    ament_lint_auto_find_test_dependencies()
    ament_add_gtest(${PROJECT_NAME}_test test/assembly_time_values_test.cpp ${${PROJECT_NAME}_SRCS})
    target_include_directories(${PROJECT_NAME}_test
            PUBLIC
            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
            $<INSTALL_INTERFACE:include>
            )
    ament_target_dependencies(
            ${PROJECT_NAME}_test
            rclcpp
            ethernetip_interface
            cpp_threading_helpers
            ament_index_cpp
    )
    rosidl_target_interfaces(${PROJECT_NAME}_test
            ${PROJECT_NAME} "rosidl_typesupport_cpp")
endif ()

ament_package()

This is the console output when building:

Starting >>> cpp_threading_helpers
Starting >>> odva_ethernetip
Finished <<< cpp_threading_helpers [4.05s]                                                                    
Starting >>> meta_cpp_helpers
Finished <<< meta_cpp_helpers [1.59s]                                                                 
Finished <<< odva_ethernetip [14.4s]                       
Starting >>> ethernetip_interface
Finished <<< ethernetip_interface [5.03s]                     
Starting >>> bluebotics_ant
--- stderr: bluebotics_ant                                
/usr/bin/ld: CMakeFiles/ant_test.dir/test/assembly_time_values_test.cpp.o: in function `bluebotics_ant::tests::AssemblyTimeValuesTest_WriteReadPLCTime_Test::TestBody()':
assembly_time_values_test.cpp:(.text+0x3bf): undefined reference to `testing::Message::Message()'
/usr/bin/ld: assembly_time_values_test.cpp:(.text+0x3f4): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/usr/bin/ld: assembly_time_values_test.cpp:(.text+0x40d): undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/usr/bin/ld: assembly_time_values_test.cpp:(.text+0x41c): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: assembly_time_values_test.cpp:(.text+0x4ec): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/usr/bin/ld: CMakeFiles/ant_test.dir/test/assembly_time_values_test.cpp.o: in function `bluebotics_ant::tests::main(int, char**)':
assembly_time_values_test.cpp:(.text+0x59b): undefined reference to `testing::InitGoogleTest(int*, char**)'
...
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-03-30 18:26:46 -0500

updated 2021-03-30 18:27:59 -0500

You didn't actually find gtest I believe, the rest looks good to me from a quick glance

find_package(ament_cmake_gtest REQUIRED)

Ex. from Nav2's CI

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
  find_package(ament_cmake_gtest REQUIRED)
  add_subdirectory(test)
endif()
edit flag offensive delete link more
0

answered 2021-03-31 08:17:25 -0500

lexi gravatar image

My IDE added my testing files to my project srcs in the cmake automatically and that's why my project failed to build. Manual tests that were not linked against gtest failed to build because they were being compiled with the project srcs which erroneously contained testing files that were using gtest.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-03-29 16:00:58 -0500

Seen: 738 times

Last updated: Mar 31 '21