0

Rosanswers logo

For some test purposes I had to define some pre-processors that create different classes for test or non-test modes. For using in a single package this approach is sufficient and we could define pre-processors definitions in CMakeLists for test or regular executable.

But If I export a library from a catkin package, I confused how could I have different libraries export form a single catkin package to able to use the test version of the library.

For example assume I have a package A:

project(A)
catkin_package(
  LIBRARIES A
)    
add_library(A src/foo.cpp src/A.cpp)    
if (CATKIN_ENABLE_TESTING)
  catkin_add_gtest(test_A test/test_A.cpp src/foo.cpp src/A.cpp)          
  target_compile_definitions(test_A PRIVATE some_macro=true)
endif()

And another B package:

project(B)    
find_package(catkin REQUIRED COMPONENTS
                    A)
catkin_package(
)    
add_executable(B src/bar.cpp src/B.cpp)
target_link_libraries(B ${catkin_LIBRARIES})    
if (CATKIN_ENABLE_TESTING)
  catkin_add_gtest(test_B test/test_B.cpp src/bar.cpp src/B.cpp)          
  target_compile_definitions(test_B PRIVATE some_macro=true)
  target_link_libraries(test_B ${catkin_LIBRARIES})
endif()

The problem arises when we want to build test_B target, we must have a test version of library A but we could not able to export another test library using catkin, in fact, we could but all of them exported at once we could not define which one must use in which case.

Is there anyway to define some customized behavior for catkin package or do you have other suggestions for have a test version of a library?


Originally posted by MohsenTamiz on ROS Answers with karma: 31 on 2017-12-09

Post score: 0

1 Answers1

0

Rosanswers logo

The exported variable <pkgname>_LIBRARIES should contain the library downstream packages are commonly supposed to use. In your case I would say the non-test library.

Your package can optionally define any arbitrary CMake variable. So it could set a variable <pkgname>_TEST_LIBRARIES. Since this is not a "standard" use case you need to do that in your own CMake file which you can register using the CFG_EXTRAS argument of the catkin_package function. Downstream packages must than choose to use that CMake variable instead in order to get the test version of your libraries.


Originally posted by Dirk Thomas with karma: 16276 on 2018-01-02

This answer was ACCEPTED on the original site

Post score: 0