ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

undefined reference to 'ros::init'

asked 2016-06-06 04:50:54 -0600

ravijoshi gravatar image

updated 2016-06-06 04:52:15 -0600

Hi, I am making a service, which takes two double array as input and returns another double array. Below is the sample server.cpp file-

#include <ros/ros.h>
#include <kinematics/test_service.h>

bool service_callback(kinematics::test_service::Request &req, kinematics::test_service::Response &res) {
  double x1 = req.param_1[0];
  double y1 = req.param_1[1];
  double z1 = req.param_1[2];

  double x2 = req.param_2[0];
  double y2 = req.param_2[1];
  double z2 = req.param_2[2];

  ROS_INFO_STREAM("Pose {" << x1 << ", " << y1 << ", " << z1 << "}");

  res.param_3.resize(3);
  res.param_3.at(0) = x1 + x2;
  res.param_3.at(1) = y1 + y2;
  res.param_3.at(2) = z1 + z2;
  return true;
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "test_service_node");
  ros::NodeHandle nh;
  ros::ServiceServer service = nh.advertiseService("test_service", service_callback);
  ros::spin();
  return 0;
}

Below is the sample client.cpp file-

#include <ros/ros.h>
#include <kinematics/test_service.h>

int main(int argc, char **argv) {
  ros::init(argc, argv, "service_node_test");
  ros::NodeHandle nh;

  ros::ServiceClient client =
      nh.serviceClient<kinematics::test_service>("test_service");

  std::vector<double> param_1(3);
  param_1.push_back(0.8047);
  param_1.push_back(0.1436);
  param_1.push_back(0.3308);

  std::vector<double> param_2(3);
  param_2.push_back(0.7966);
  param_2.push_back(0.1354);
  param_2.push_back(0.3337);

  kinematics::test_service service;
  service.request.param_1 = param_1;
  service.request.param_2 = param_2;

  if (client.call(service)) {
    ROS_INFO_STREAM("Clinet Response " << service.response.param_3.size());
  } else {
    ROS_ERROR("Failed to call test_service");
    return -1;
  }

  return 0;
}

The CMakeLists.txt file has following content-

cmake_minimum_required(VERSION 2.8.3)
project(service_test)

find_package(catkin REQUIRED COMPONENTS)

catkin_package(
LIBRARIES
CATKIN_DEPENDS
)

include_directories(include
${catkin_INCLUDE_DIRS}
)

add_executable(server src/server.cpp)
add_executable(client src/client.cpp)

target_link_libraries(server ${catkin_LIBRARIES})
target_link_libraries(client ${catkin_LIBRARIES})

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

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

install(DIRECTORY launch/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
PATTERN ".svn" EXCLUDE)

While compiling the above code using catkin_make, it is throwing following error-

server.cpp:(.text+0x61): undefined reference to `ros::init(int&, char**, std::string const&, unsigned int)'
server.cpp:(.text+0xbd): undefined reference to `ros::NodeHandle::NodeHandle(std::string const&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
server.cpp:(.text+0x14a): undefined reference to `ros::spin()'
server.cpp:(.text+0x15e): undefined reference to `ros::ServiceServer::~ServiceServer()'

How to solve this error?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
4

answered 2016-06-06 04:55:49 -0600

gvdhoorn gravatar image

updated 2016-06-06 05:01:03 -0600

You're not linking against roscpp, which is the library that provides those symbols.

How to solve this error?

Link against roscpp. Add it to the find_package(catkin COMPONENTS ..) statement, and add it to your catkin_package(.. CATKIN_DEPENDS ..) list.

I'd also make your CMakeLists.txt search for your kinematics package, as you use that in your service server definitions.


Edit: this is also discussed in the Writing a Simple Publisher and Subscriber (C++) - Building your nodes tutorial, and see the catkin documentation » How to do common tasks » Package format 2 (recommended) » C++ catkin library dependencies for how to update your CMakeLists.txt and package manifest.

Note also that this is not a ROS specific problem, but a standard C++ linking error, and that it has been asked many times before.

edit flag offensive delete link more

Comments

Thanks . It worked.

ravijoshi gravatar image ravijoshi  ( 2016-06-06 06:22:06 -0600 )edit

I am facing a similar issue. I edited the CMakeLists.txt as suggested above. But I also had to edit the package.xml to add roscpp as run and build dependency. Is this right way to do it or does linking roscpp requires me to add any additional #include...... statements in the source file ?

skr_robo gravatar image skr_robo  ( 2016-07-14 11:58:29 -0600 )edit
1

But I also had to edit the package.xml [..]

Yes, that is what I meant with "update your [..] package manifest": package.xml is the manifest.

And, no. Provided you are not using anything not already included by ros.h, you don't need to add any additional #includes.

gvdhoorn gravatar image gvdhoorn  ( 2016-07-14 13:17:25 -0600 )edit

Thank You.

skr_robo gravatar image skr_robo  ( 2016-07-15 14:04:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-06 04:50:54 -0600

Seen: 6,659 times

Last updated: Jun 06 '16