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

Revision history [back]

click to hide/show revision 1
initial version

Hi @Cry-A-Lot,

The recommended link you posted explains you how to compile a target against a library of the same package.

The first recommendation is that you should not include cpp files in your code but hpp or or h (header) since with that intruction (E.g.: #include <pkg_a/mutiply.cpp>) the compiler will probably throw an error because of the multiples and function duplications.

After you have generated properly your headers you should compile your targets against your libraries. It means that, if for instance you have in sumfunc.cpp something you want to use in main.cpp you should compile your main.cpp as:

First you have to tell to catkin your dependencies:

find_package(catkin REQUIRED COMPONENTS roscpp pkg_a)

Next include all what you need:

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
  ...
)

Then tell cmake to gather all headers:

catkin_package(
  INCLUDE_DIRS include
  CATKIN_DEPENDS pkg_dependencies
  DEPENDS pkg_external_dependencies
)

Then build like:

add_executable(main_node src/main.cpp) target_link_libraries(main_node ${catkin_LIBRARIES}) add_dependencies(main_node ${catkin_EXPORTED_TARGETS})

Finally declare the dependency on the package.xml file like:

<build_depend>pkg_a</build_depend>
<build_export_depend>pkg_a</build_export_depend>
<exec_depend>pkg_a</exec_depend>

This is done assuming you use headers, since in the main.cpp you will do something like #include <pkg_a/sumfunc.h>

Hope that solve your problem.

Regards.

Hi @Cry-A-Lot,

The recommended link you posted explains you how to compile a target against a library of the same package.

The first recommendation is that you should not include cpp files in your code but hpp or or h (header) since with that intruction (E.g.: #include <pkg_a/mutiply.cpp>) the compiler will probably throw an error because of the multiples and function duplications.

After you have generated properly your headers you should compile your targets against your libraries. It means that, if for instance you have in sumfunc.cpp something you want to use in main.cpp you should compile your main.cpp as:

First you EDIT:

I have managed to tell to catkin your dependencies:achieve what you want, first of all the package structure is the following:

└── src
    ├── pkg_a
    │   ├── CMakeLists.txt
    │   ├── include
    │   │  ├── package_a
    │   │  │   ├── some_func.hpp
    │   ├── src
    │   │  ├── some_func.cpp
    │   └── package.xml
    └── pkg_b
    │   ├── CMakeLists.txt
    │   ├── src
    │   │   ├── main.cpp
    │   └── package.xml

Then in CmakeList.txt of package_a:

find_package(catkin REQUIRED COMPONENTS roscpp pkg_a)

Next include all what you need:

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
COMPONENTS
  roscpp
  std_msgs
  ...
)

Then tell cmake to gather all headers:

 catkin_package(
  INCLUDE_DIRS include
  CATKIN_DEPENDS pkg_dependencies
  DEPENDS pkg_external_dependencies
LIBRARIES SumFunc
)

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

add_library(SumFunc
   src/sum_func.cpp
)

Then build like:

And to use in package_b:

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  package_a
  ...
)

catkin_package()

include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_executable(main_node src/main.cpp)
target_link_libraries(main_node ${catkin_LIBRARIES})
add_dependencies(main_node ${catkin_EXPORTED_TARGETS})

Finally declare the dependency on the package.xml file like:

<build_depend>pkg_a</build_depend>
<build_export_depend>pkg_a</build_export_depend>
<exec_depend>pkg_a</exec_depend>

target_link_libraries(main_node
  ${catkin_LIBRARIES}
)

This is done assuming you use headers, since in the main.cpp you will do something like #include <pkg_a/sumfunc.h>

Hope that solve your problem.

Regards.