Ask Your Question
0

[ROS2] correct way to link to created library in gtest

asked 2020-01-08 03:19:55 -0600

frpunzalan gravatar image

Hi,

I am trying to create some sample unit tests for the package library I am creating. It looks like the library is being created but I get an undefined reference error. it looks like the test code couldn't link to the library.

CMakeFiles/configurator-test.dir/test/test_configurator.cpp.o: In function `ConfiguratorTest::SetUp()':
test_configurator.cpp:(.text._ZN16ConfiguratorTest5SetUpEv[_ZN16ConfiguratorTest5SetUpEv]+0x35): undefined reference to `object_analysis::Configurator::Configurator()'
CMakeFiles/configurator-test.dir/test/test_configurator.cpp.o: In function `ConfiguratorTest::TearDown()':
test_configurator.cpp:(.text._ZN16ConfiguratorTest8TearDownEv[_ZN16ConfiguratorTest8TearDownEv]+0x3b): undefined reference to `object_analysis::Configurator::~Configurator()'
CMakeFiles/configurator-test.dir/test/test_configurator.cpp.o: In function `ConfiguratorTest::MatchString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
test_configurator.cpp:(.text._ZN16ConfiguratorTest11MatchStringERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN16ConfiguratorTest11MatchStringERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x33): undefined reference to `object_analysis::Configurator::get_string[abi:cxx11]()'
collect2: error: ld returned 1 exit status

I have a near empty class that has a function that returns a string and a test that checks the value of the returned string. The cmakelist file and package file are shown below:

CMakeList

cmake_minimum_required(VERSION 3.5)
project(configurator)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

include_directories(include)

set(CONFIGURATOR_LIB_SRC
    src/configurator.cpp
)

set(CONFIGURATOR_LIB_HEADERS
    include/configurator/configurator.hpp)

add_library(
${PROJECT_NAME} SHARED
        ${CONFIGURATOR_LIB_SRC}
        ${CONFIGURATOR_LIB_HEADERS}
)

add_executable(configurator_node src/configurator_node.cpp)
target_include_directories(configurator_node PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

install(TARGETS configurator_node
  EXPORT export_${PROJECT_NAME}
  DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  find_package(ament_cmake_gtest REQUIRED)

  # exclude some default linters from ament_lint
  list(APPEND AMENT_LINT_AUTO_EXCLUDE
    ament_cmake_uncrustify
  )
  list(APPEND AMENT_LINT_AUTO_EXCLUDE
    ament_cmake_copyright
  )
  ament_lint_auto_find_test_dependencies()

  # Add a gtest executables
  ament_add_gtest(${PROJECT_NAME}-test
    test/test_configurator.cpp
  )
  target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})

endif()  # BUILD_TESTING

ament_package()

package.xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>configurator</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="root@todo.todo">root</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake_auto</buildtool_depend>
  <depend>rclcpp</depend>

  <test_depend>ament_cmake</test_depend>
  <test_depend>ament_cmake_gtest</test_depend>
  <test_depend>ament_lint_auto</test_depend>
  <test_depend>rclcpp</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>
edit retag flag offensive close merge delete

Comments

My bad. I actually had a mistake in the code. Forgot to put namespace in the cpp file. Sorry about that. The linking seems to be fine. I still want to know how to link using ament for gtest. I am using the cmake way for linking. Is there another way to link using ament commands?

frpunzalan gravatar imagefrpunzalan ( 2020-01-08 03:48:04 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-01-08 11:59:27 -0600

jacobperron gravatar image

To answer your question in the comment, you can use ament_target_dependencies to link against other ament packages (for example). But if you are linking against a target in the same project, there's no ament command that I am aware of. target_link_libraries is the way to do it (as you have done in your posted cmake code).

edit flag offensive delete link more

Comments

thanks. i will use that when i link packages.

frpunzalan gravatar imagefrpunzalan ( 2020-01-08 19:35:23 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-01-08 03:19:55 -0600

Seen: 30 times

Last updated: Jan 08