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?
EDIT: add CMakeLists
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
add_message_files(
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}
)
add_executable(test_input src/test_input.cpp)
target_link_libraries(test_input ${PROJECT_NAME})
add_dependencies(test_input ${PROJECT_NAME}_gencfg)
add_executable(test_motion_estimation src/test_motion_estimation.cpp)
target_link_libraries(test_motion_estimation ${PROJECT_NAME})
add_dependencies(test_motion_estimation ${PROJECT_NAME}_gencfg)
add_executable(test_person_attention src/test_person_attention.cpp)
target_link_libraries(test_person_attention ${PROJECT_NAME})
add_dependencies(test_person_attention ${PROJECT_NAME}_gencfg)
add_executable(continuous_input_stub src/continuous_input_stub.cpp)
target_link_libraries(continuous_input_stub ${PROJECT_NAME})
Can you post your cmakelists.txt?
Yes of course. I have updated the question.
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 thecatkin_package
of social_layers underLIBRARIES
? 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 theadd_library
andcatkin_package
from social_layers as wellThese come from the CMakelists of
social_layers
:Thank you very much for your help. If you need something more please tell me, I'm not so good with CMake.
UPDATE: By modifying the CMakelists of social_layers in this way:
I was able to compile, but I get this warning:
That may lead to this runtime error:
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 theLIBRARIES
tag in thecatkin_package
. This would be needed if you did have another library already namedsocial_layers
.Are you using
catkin build
orcatkin_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
, andlogs
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 ofcatkin build
). Let me know how it goes.Well, what happened is really weird. I just followed your suggestion and I have kept the library name as
person
. After building withcatkin 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.
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 namesocial_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).