undefined reference to 'ros::init'
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?