# Linking problems with a classes in different packages

Hi,

I have defined the class Person in the social_layers package. Now I want to use it in a different package. So I have included the header in this way:

#include "../../../social_layers/include/social_layers/person.hpp"


And then is use it in the code. Unfortunately, I got this error:

/home/alberto/tiago_dual_public_ws/devel/.private/new_policies_system/lib/libnew_policies_system.so: undefined reference to Person::Person(int, unsigned char, double, geometry_msgs::Pose_<std::allocator<void> >)'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/alberto/tiago_dual_public_ws/devel/.private/new_policies_system/lib/new_policies_system/test_motion_estimation] Error 1
make[1]: *** [CMakeFiles/test_motion_estimation.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
/home/alberto/tiago_dual_public_ws/devel/.private/new_policies_system/lib/libnew_policies_system.so: undefined reference to Person::Person(int, unsigned char, double, geometry_msgs::Pose_<std::allocator<void> >)'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/alberto/tiago_dual_public_ws/devel/.private/new_policies_system/lib/new_policies_system/test_input] Error 1
make[1]: *** [CMakeFiles/test_input.dir/all] Error 2
/home/alberto/tiago_dual_public_ws/devel/.private/new_policies_system/lib/libnew_policies_system.so: undefined reference to Person::Person(int, unsigned char, double, geometry_msgs::Pose_<std::allocator<void> >)'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/alberto/tiago_dual_public_ws/devel/.private/new_policies_system/lib/new_policies_system/test_person_attention] Error 1
make[1]: *** [CMakeFiles/test_person_attention.dir/all] Error 2
make: *** [all] Error 2


In the original package, I get no errors. Can someone help me to understand why?

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") cmake_minimum_required(VERSION 3.0.2) project(new_policies_system) find_package(catkin REQUIRED COMPONENTS roscpp std_msgs nav_msgs map_msgs message_generation grid_map_ros costmap_2d eigen_conversions dynamic_reconfigure continuous_input grid_map_core grid_map_ros grid_map_cv grid_map_filters grid_map_loader grid_map_msgs grid_map_octomap grid_map_rviz_plugin grid_map_visualization geometry_msgs sensor_msgs cv_bridge octomap_msgs filters motion_estimation social_layers gazebo_person_detection ) find_package(cmake_modules REQUIRED) find_package(Eigen3 QUIET) if(NOT EIGEN3_FOUND) find_package(PkgConfig REQUIRED) pkg_check_modules(EIGEN3 REQUIRED eigen3) set(EIGEN3_INCLUDE_DIR${EIGEN3_INCLUDE_DIRS})
endif()

## Generate messages in the 'msg' folder
FILES
continuous_input.msg
)

## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)

include_directories(include ${catkin_INCLUDE_DIRS}${EIGEN3_INCLUDE_DIRS}})

generate_dynamic_reconfigure_options (
cfg/Policy.cfg
)

catkin_package(
INCLUDE_DIRS
include
LIBRARIES
${PROJECT_NAME} CATKIN_DEPENDS roscpp message_runtime DEPENDS EIGEN3 ) add_library(new_policies_system src/Policy.cpp src/MathUtilities.cpp src/GeometryUtilities.cpp src/Input.cpp src/MotionEstimation.cpp src/PersonAttention.cpp src/UserIntention.cpp ) target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}${EIGEN3_LIBRARIES}
)

add_dependencies(${PROJECT_NAME}${catkin_EXPORTED_TARGETS}
)

target_link_libraries(test_input ${PROJECT_NAME}) add_dependencies(test_input${PROJECT_NAME}_gencfg)

target_link_libraries(test_motion_estimation ${PROJECT_NAME}) add_dependencies(test_motion_estimation${PROJECT_NAME}_gencfg)

target_link_libraries(test_person_attention ${PROJECT_NAME}) add_dependencies(test_person_attention${PROJECT_NAME}_gencfg)

target_link_libraries(continuous_input_stub ${PROJECT_NAME})  edit retag close merge delete ## Comments Can you post your cmakelists.txt? ( 2021-06-05 10:33:05 -0500 )edit Yes of course. I have updated the question. ( 2021-06-05 10:47:21 -0500 )edit Are you building a person library or adding it to an existing library target in social_layers? If so, is its library name being added in the catkin_package of social_layers under LIBRARIES? I would also change your include statement to #include <social_layers/person.hpp> as your new package and social_layers may not always have the same relative path between them. If you can, post the add_library and catkin_package from social_layers as well ( 2021-06-05 11:40:27 -0500 )edit These come from the CMakelists of social_layers: catkin_package( # INCLUDE_DIRS include #LIBRARIES social_layers CATKIN_DEPENDS pal_detection_msgs # DEPENDS system_lib ) add_library(person src/person.cpp )  Thank you very much for your help. If you need something more please tell me, I'm not so good with CMake. ( 2021-06-06 03:19:15 -0500 )edit UPDATE: By modifying the CMakelists of social_layers in this way: catkin_package( # INCLUDE_DIRS include LIBRARIES social_layers CATKIN_DEPENDS pal_detection_msgs # DEPENDS system_lib ) add_library(social_layers src/person.cpp )  I was able to compile, but I get this warning: Warning: Cannot symlink from /home/alberto/tiago_dual_public_ws/devel/.private/social_layers/lib/libsocial_layers.so to existing file /home/alberto/tiago_dual_public_ws/devel/lib/libsocial_layers.so  That may lead to this runtime error: /home/alberto/tiago_dual_public_ws/devel/lib/new_policies_system/test_person_attention: symbol lookup error: /home/alberto/tiago_dual_public_ws/devel/lib/libnew_policies_system.so: undefined symbol: _ZN6PersonC1EihdN13geometry_msgs5Pose_ISaIvEEE  ( 2021-06-06 03:55:41 -0500 )edit Great! That is what we were looking for. I think you are passed the initial linking problem. For reference, you could have kept the library name as person and just added that after the LIBRARIES tag in the catkin_package. This would be needed if you did have another library already named social_layers. Are you using catkin build or catkin_make. I would be interested in seeing what your build command looks like if you are using any configuration options. This new problem looks to be an issue with catkin_tools based on this question. Can you delete your build, devel, and logs folder and try building again. If that does not work, update catkin_tools with pip install catkin_tools --upgrade. If that does not work, try switching build systems (ie use catkin_make instead of catkin build). Let me know how it goes. ( 2021-06-06 10:19:45 -0500 )edit Well, what happened is really weird. I just followed your suggestion and I have kept the library name as person. After building with catkin build <package_name> I got the same warning, but now I have no errors in runtime. Perhaps there was some buffered information that have been deleted when I restarted the PC to re-try. ( 2021-06-06 11:58:32 -0500 )edit I would still try clearing your folders and updating catkin_tools. Switching to catkin_make would be to confirm the issue was due to catkin_tools. If the name social_layers was used for another library target or executable target then that might explain the runtime error (essentially linking to the wrong library because of the name clash). ( 2021-06-06 12:19:02 -0500 )edit ## 1 Answer Sort by » oldest newest most voted Compiling the comments into an answer: Package: new_policies_system CMakeLists.txt should include: find_package(catkin REQUIRED COMPONENTS roscpp social_layers ... ) catkin_package( INCLUDE_DIRS include LIBRARIES new_policies_system CATKIN_DEPENDS roscpp social_layers ... DEPENDS EIGEN3 ) include_directories( include${catkin_INCLUDE_DIRS}
...
)

src/Policy.cpp
...
)

${catkin_LIBRARIES}${EIGEN3_LIBRARIES}
)

${catkin_EXPORTED_TARGETS} )  Note I have replaced {PROJECT_NAME} with new_policies_system to be consistent across the library, linking, and dependencies. package.xml should include: <depend>social_layers</depend>  Package: social_layers CMakeLists.txt should include: catkin_package( INCLUDE_DIRS include LIBRARIES person CATKIN_DEPENDS pal_detection_msgs ) add_library(person src/person.cpp ) target_link_libraries(person${catkin_LIBRARIES}
)


Note I have added a target_link_libraries for the person library. If you use any external code in person then you should keep this call in there. Only add the names of libraries you are creating in this package to the catkin_package LIBRARIES section. IE do NOT keep social_layers in there unless you have an add_library(social_layers src/path/to/code.cpp)`

Final note when making changes to library names and CMakeLists in general. If you are experiencing strange behavior, try clearing out devel, build, and log directories. Just because you changed the name of something and recompile, the original probably still exists with the wrong name.

The ROS wiki has in depth info as well and is worth a read.

more

Very helpful, thank you so much! Now it seems all ok!

( 2021-06-08 11:42:48 -0500 )edit