Ask Your Question
2

How to use a service defined in another package ?

asked 2016-08-25 09:37:19 -0500

roach gravatar image

Hi all,

I am facing a compilation problem when I try to include a service.h file in a node located in another package. Here is the architecture :

rosfond/
    src/
        my_nodes.cpp
    include/
        ...
    srv/
        my_service.srv
hmi/
    src/
        my_other_nodes.cpp
    include/
        ...

I want to use my_service in the hmi package, and especially include my_service.h in my_other_nodes.cpp. For that, I wrote at the beginning of my_other_nodes.cpp #include "rosfond/my_service.h". I also use my_service in my_nodes.

When I try to compile my whole workspace, it fails. However, if I compile only rosfond, it works. So the problem seems to be related to how I include/link my_service in my_other_nodes and in the hmi's CMakeLists.txt.

Here are the CMakeLists.txt and the package.xml file of each package :

cmake_minimum_required(VERSION 2.8.3)
project(rosfond)
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)
add_service_files(DIRECTORY srv FILES my_service.srv)
generate_messages(DEPENDENCIES std_msgs)
catkin_package(
 INCLUDE_DIRS include
#  LIBRARIES rosfond
 CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)
include_directories(
  ${catkin_INCLUDE_DIRS} 
  include
)
add_executable(my_nodes src/my_nodes.cpp)
target_link_libraries(my_nodes
  ${catkin_LIBRARIES}
)

+++++++++++++++++++++++++++++++++++++++++++++++

<?xml version="1.0"?>
<package>
  <name>rosfond</name>
  <version>0.0.0</version>
  <description>The rosfond package</description>

  <maintainer email="ghost@todo.todo">ghost</maintainer>

  <license>TODO</license>

  <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>

+++++++++++++++++++++++++++++++++++++++++++++++

cmake_minimum_required(VERSION 2.8.3)
project(hmi)
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)
generate_messages(DEPENDENCIES std_msgs)
catkin_package(
 INCLUDE_DIRS include
#  LIBRARIES hmi
 CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)
include_directories(
  ${catkin_INCLUDE_DIRS} 
  include
)
add_executable(my_other_nodes src/my_other_nodes.cpp)
target_link_libraries(my_other_nodes
  ${catkin_LIBRARIES}
)

+++++++++++++++++++++++++++++++++++++++++++++++

<?xml version="1.0"?>
<package>
  <name>hmi</name>
  <version>0.0.0</version>
  <description>The hmi package</description>

  <maintainer email="ghost@todo.todo">ghost</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>rosfond</build_depend>
  <run_depend>rosfond</run_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>rospy</run_depend>
  <run_depend>std_msgs</run_depend>
</package>

++++++++++++++++++++++++++++++++++++++++++++++++

The error message is : my_other_nodes.cpp : rosfond/my_service.h: No such file or directory.

Thanking you in advance for your answers,

roach

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-08-31 03:15:31 -0500

roach gravatar image

updated 2017-01-07 11:16:55 -0500

130s gravatar image

Hi all,

After some research, I have found one solution to my problem. I have added add_dependencies(my_nodes ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) to the rosfond's CMakeLists.txt (only for the nodes that use my_service) as well as in the hmi's one (my_nodeschanged into my_other_nodes obviously). I have also find_package-d rosfond in hmi's CMakeLists.txt. In the rosfond's package.xml, I have added the lines

<export>
    <cpp cflags="-I${prefix}/srv/cpp"/>
</export>

And the the hmi's one :

<run_depend>rosfond</run_depend>
<build_depend>rosfond</build_depend>

Thanks to these changes, I could include rosfond/my_service.h in my_other_nodes.cpp.

I hope this solution will work for you if you have the same problem.

edit flag offensive delete link more

Comments

3

That is basically how it should be done, but the cpp flags export is not needed / undesirable. That is a rosbuild thing (old build system). Catkin doesn't use those anymore (it's basically CMake). See http://docs.ros.org/indigo/api/catkin...

gvdhoorn gravatar imagegvdhoorn ( 2016-08-31 04:39:42 -0500 )edit
1

I second to @gvdhoorn. I crossed out the unnecessary portion in the body to avoid potential confusion for future readers.

130s gravatar image130s ( 2017-01-07 11:18:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-08-25 09:37:19 -0500

Seen: 1,777 times

Last updated: Jan 07 '17