How to build gtest tests in ros2?
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**)'
...