0
$\begingroup$

Rosanswers logo

Dear All,

I've created a simple catkin library including a class with a member function (lib_test). I want to use this class/library in another catkin pkg (caller_pkg). I receive this error :

CMakeFiles/main2.dir/src/main2.cpp.o: In function main': main2.cpp:(.text+0x23): undefined reference to Utility::dosomething(int)' collect2: error: ld returned 1 exit status caller_pkg/CMakeFiles/main2.dir/build.make:113: recipe for target '/home/alireza/catkin_ws/devel/lib/caller_pkg/main2' failed

The pkg contents are as follows.

******** the library pkg <lib_test> ******

 cmake_minimum_required(VERSION 2.8.3)
 project(lib_test)
 find_package(catkin REQUIRED COMPONENTS
 roscpp
 rospy
 std_msgs)

 catkin_package(
 INCLUDE_DIRS include
 LIBRARIES lib_test
 CATKIN_DEPENDS roscpp rospy std_msgs)

 include_directories(include
  ${catkin_INCLUDE_DIRS}
  )

 add_library(lib_test src/utility.cpp )

 target_link_libraries(lib_test ${catkin_LIBRARIES})

 install(TARGETS lib_test 
 ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
 LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
 RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

 install(DIRECTORY include/${PROJECT_NAME}/
 DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION})

package.xml

  <?xml version="1.0"?>
  <package>
  <name>lib_test</name>
  <version>0.0.0</version>
  <description>The lib_test package</description>
  <maintainer email="[email protected]">alireza</maintainer>
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>rospy</run_depend>
  <run_depend>std_msgs</run_depend>
  <package>

lib_test/include/lib_test/utility.h

  #ifndef _COMMON_UTILITY_
  #define _COMMON_UTILITY_

  class Utility
  {
    public:
            int dosomething(int param);
  };
 #endif  

src/utility.cpp:

#include "lib_test/utility.h"

int Utility::dosomething(int param)
{
   return param / 2;
 }

The pkg that calls library: caller_pkg

  cmake_minimum_required(VERSION 2.8.3)
  project(caller_pkg)

  find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  )
  find_package(lib_test REQUIRED)
  catkin_package(
  # INCLUDE_DIRS include
  # LIBRARIES lib_test
   CATKIN_DEPENDS roscpp rospy std_msgs lib_test
  # DEPENDS lib_test
  )
  include_directories(
  ${catkin_INCLUDE_DIRS}
  )

  include_directories(
  ${lib_test_INCLUDE_DIRS}
  )
  add_executable(main2 src/main2.cpp)

  target_link_libraries(main2
  ${catkin_LIBRARIES}
  ${lib_test_INCLUDE_DIRS}
  )

   install(DIRECTORY include/${PROJECT_NAME}/
   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
   #  FILES_MATCHING PATTERN "*.h"
   #  PATTERN ".svn" EXCLUDE
   )

   install(TARGETS main2
   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

package.xml

 <?xml version="1.0"?>
 <package>
 <name>caller_pkg</name>
 <version>0.0.0</version>
 <description>The caller_pkg package</description>
 <maintainer email="[email protected]">alireza</maintainer>
 <buildtool_depend>catkin</buildtool_depend>
 <build_depend>roscpp</build_depend>
 <build_depend>rospy</build_depend>
 <build_depend>std_msgs</build_depend>
 <run_depend>roscpp</run_depend>
 <run_depend>rospy</run_depend>
 <run_depend>std_msgs</run_depend>
 <package>

src/main2.cpp:

  #include <ros/ros.h>
  #include "lib_test/utility.h"
  int main(int argc, char** argv)
  {
  ros::init(argc, argv, "lib_test_node");
  ros::start();
  Utility u;
  int x = u.dosomething(26);
  ROS_INFO("x=%1d",x);
  ros::spin();
  ros::shutdown();
  return 0;}

I would appreciate it if you help me out.


Originally posted by A.M Dynamics on ROS Answers with karma: 93 on 2016-07-28

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The CMakeLists.txt of the caller_pkg has a typo, this:

  target_link_libraries(main2
  ${catkin_LIBRARIES}
  ${lib_test_INCLUDE_DIRS}
  )

Should use lib_test_LIBRARIES instead:

  target_link_libraries(main2
  ${catkin_LIBRARIES}
  ${lib_test_LIBRARIES}
  )

Originally posted by William with karma: 17335 on 2016-07-28

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by A.M Dynamics on 2016-07-28:
@William: Thanks, it is working now

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.