Undefined reference error despite linking the library [closed]
Hi,
I am using ROS Hydro on Ubuntu 12.04. I have a package named as libbarrett_ros. The declaration of the class arm_interface is in include/barrett_arm.h and the definition is in src/barrett_arm.cpp. The executable node (src/libbarrett_ros.cpp) creates an instance of the class barrett_arm_interface in its main function.
However, catkin_make returns the following error for me,
Linking CXX executable /home/robot/catkin_ws/devel/lib/libbarrett_ros/libbarrett_ros
CMakeFiles/libbarrett_ros.dir/src/libbarrett_ros.cpp.o: In function `int wam_main<3u>(int, char**, barrett::ProductManager&, barrett::systems::Wam<3u>&)':
libbarrett_ros.cpp:(.text._Z8wam_mainILj3EEiiPPcRN7barrett14ProductManagerERNS2_7systems3WamIXT_EEE[int wam_main<3u>(int, char**, barrett::ProductManager&, barrett::systems::Wam<3u>&)]+0x14): undefined reference to `barrett_arm_interface<3u>::barrett_arm_interface()'
CMakeFiles/libbarrett_ros.dir/src/libbarrett_ros.cpp.o: In function `int wam_main<4u>(int, char**, barrett::ProductManager&, barrett::systems::Wam<4u>&)':
libbarrett_ros.cpp:(.text._Z8wam_mainILj4EEiiPPcRN7barrett14ProductManagerERNS2_7systems3WamIXT_EEE[int wam_main<4u>(int, char**, barrett::ProductManager&, barrett::systems::Wam<4u>&)]+0x14): undefined reference to `barrett_arm_interface<4u>::barrett_arm_interface()'
CMakeFiles/libbarrett_ros.dir/src/libbarrett_ros.cpp.o: In function `int wam_main<7u>(int, char**, barrett::ProductManager&, barrett::systems::Wam<7u>&)':
libbarrett_ros.cpp:(.text._Z8wam_mainILj7EEiiPPcRN7barrett14ProductManagerERNS2_7systems3WamIXT_EEE[int wam_main<7u>(int, char**, barrett::ProductManager&, barrett::systems::Wam<7u>&)]+0x14): undefined reference to `barrett_arm_interface<7u>::barrett_arm_interface()'
collect2: ld returned 1 exit status
make[2]: *** [/home/robot/catkin_ws/devel/lib/libbarrett_ros/libbarrett_ros] Error 1
make[1]: *** [libbarrett_ros/CMakeFiles/libbarrett_ros.dir/all] Error 2
make: *** [all] Error 2
Invoking "make" failed
I know that this is a problem with linking the barrett_arm with my executable node. I have added the barrett_arm.cpp and linked the target against my executable node. But that does not solve the problem either. Please let me know what I am missing here. Thanks in advance.
My CMakeLists.txt is as below
cmake_minimum_required(VERSION 2.8.3)
project(libbarrett_ros)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
find_package(Barrett REQUIRED)
find_package(Eigen REQUIRED)
catkin_package(
INCLUDE_DIRS include
CATKIN_DEPENDS
message_runtime
wam_msgs
LIBRARIES ${PROJECT_NAME}
)
include_directories(include
${catkin_INCLUDE_DIRS}
)
link_directories(${BARRETT_LIB_DIRS})
include_directories(${BARRETT_INCLUDE_DIRS})
add_definitions(${BARRETT_DEFINITIONS})
add_library(barrett_arm src/barrett_arm.cpp)
target_link_libraries(barrett_arm ${catkin_LIBRARIES} ${BARRETT_LIBRARIES})
add_executable(${PROJECT_NAME} src/libbarrett_ros.cpp)
target_link_libraries(${PROJECT_NAME} barrett_arm ${catkin_LIBRARIES} ${BARRETT_LIBRARIES})
add_dependencies(${PROJECT_NAME} ${catkin_EXPORTED_TARGETS} wam_msgs_gencpp)
The
barrett_arm_interface
class looks like a template class, and it looks like you're failing to link against a specific instantiation of that template. Is that class part of your library or part of the barret library you're linking to?Yes, the
barrett_arm_interface
class is a template class. This is part of my library. I have written my own template class that gets instantiated with the DOF value which is 3u, 4u and 7u passed fromlibbarrett_ros.cpp
.Initially, I had
barrett_arm_interface
template class defined and declared within libbarrett_ros.cpp. And in the main function I was creating an instance of it and initializing the class as now. It all worked fine. But now when I moved that class as a separate source file, I am facing this problemThank you so much. You saved my day, I didnt think anything beyond "catkin linking error".