Ask Your Question
0

How can I reference an external project with Catkin?

asked 2014-08-06 18:11:18 -0500

wpd gravatar image

I would like to build a ROS node that uses some code from an external project. I certainly need to reference some header files from that project, and will probably also need to "pull in" some C++ source code from that project.

The simplest, dumbest, least maintainable way I could to this would be to simply copy the header file(s) and source file(s) into my ROS project.

I feel like there should be some way to tell Catkin or Cmake "Add this directory to the list of includes" and "Add that directory to VPATH". But, as Catkin and Cmake are totally new to me (as of about a week ago), I haven't the foggiest idea how to ask the Google to find the answer to my question. So I'll ask the community instead :-)

Thanks for any tips you can give me.

edit retag flag offensive close merge delete

Comments

Do the external projects use cmake? If they provide a cmake config file, you can find_package them just like opencv: http://wiki.ros.org/vision_opencv

jbinney gravatar image jbinney  ( 2014-08-06 19:53:35 -0500 )edit

Unfortunately, it does not. It is a traditional Make based project. I would like to pull a few header files and source files from that project into mine.

wpd gravatar image wpd  ( 2014-08-06 20:15:19 -0500 )edit

So far, I have started down the path of writing my own "Findtrunk.cmake" file to find the project in my source tree (which I keep separate from my budding ROS source tree... for the moment) ,defining ${TRUNK_DIR} in Findtrunk.cmake, and adding ${TRUNK_DIR}/include to include_directories().

wpd gravatar image wpd  ( 2014-08-06 20:18:11 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-08-06 20:53:41 -0500

wpd gravatar image

All right, here is what I did. I feel like it is tremendously hacky, but it appears to work.

I created a cmake_modules/Findtrunk.cmake file containing:

#invented based on what I found in libfreenect/cmake_modules/Findlibusb-1.0.cmake
if (TRUNK_DIR)
  # in cache already
  set(TRUNK_FOUND TRUE)
else (TRUNK_DIR)
  find_path(TRUNK_DIR
    NAMES
    rcp/Makefile
    PATHS
        $ENV{HOME}/src/hai/trunk
  )

  if (TRUNK_DIR)
    set(TRUNK_FOUND TRUE)
  endif (TRUNK_DIR)

  if (TRUNK_FOUND)
    if (NOT trunk_FIND_QUIETLY)
      message(STATUS "Found trunk:")
      message(STATUS " - ${TRUNK_DIR}")
    endif (NOT trunk_FIND_QUIETLY)
  else (TRUNK_FOUND)
    if (trunk_FIND_REQUIRED)
      message(FATAL_ERROR "Could not find trunk")
    endif (trunk_FIND_REQUIRED)
  endif (TRUNK_FOUND)

endif (TRUNK_DIR)

I added the following 2 snippets to my CMakeLists.txt file:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/")
find_package(trunk REQUIRED)

and

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${TRUNK_DIR}/common/include
  ${TRUNK_DIR}/this/that
  ${TRUNK_DIR}/the/other/thing
)
set(BBR_FILES
  ${TRUNK_DIR}/this/that/buffer.c
  ${TRUNK_DIR}/this/that/config.c
)
set_source_files_properties(${BBR_FILES} PROPERTIES LANGUAGE CXX)

add_executable(mp2ros_node
  src/mp2ros_node.cpp
  ${BBR_FILES}
)

and that seems to work. I'm not sure it's the most straightforward thing to do. I like the idea of creating a Findtrunk.cmake file so that others on my team might edit it to reflect the location of their trunk directory. We'll see if that ever comes to pass.

I would appreciate any questions, comments, or snide remarks more experienced folks would care to make regarding this approach. I'm certain that I'm not following any number of standard Cmake conventions (I noticed that other Findxxx.cmake files define INCLUDE_XXX_DIRS, for example), but this seems to work for me so far.

edit flag offensive delete link more

Comments

This seems like a good approach. Thanks for posting this!

jbinney gravatar image jbinney  ( 2014-08-07 02:06:33 -0500 )edit

Setting CMAKE_MODULE_PATH is forbidden in the ROS CMake Coding Standards document, although it doesn't say what folks should do in this case.

zultron gravatar image zultron  ( 2018-07-18 19:05:36 -0500 )edit

I don't recall why that was added to the docs ( https://github.com/ros/catkin/commit/... ). Maybe the original author remembers?

Dirk Thomas gravatar image Dirk Thomas  ( 2018-08-03 13:11:40 -0500 )edit

The approach to extend the CMAKE_MODULE_PATH locally in your CMake seems fine to me.

Dirk Thomas gravatar image Dirk Thomas  ( 2018-08-03 13:12:14 -0500 )edit
1

answered 2014-08-07 04:25:20 -0500

Chrissi gravatar image

If you want to use an external project you should never pull in c++ code. Just make it a library and use pkg-config. Have a look at this page on how to write pc files (very easy).

Afterwards you can just use:

find_package(PkgConfig)
pkg_check_modules(ARBITRARYNAME my_library-1.0)
...
include_directories(
 include
 ${catkin_INCLUDE_DIRS}
 ${ARBITRARYNAME_INCLUDE_DIRS}
)
...
target_link_libraries(my_node
 ${catkin_LIBRARIES}
 ${ARBITRARYNAME_LIBRARIES}
)
edit flag offensive delete link more

Comments

Thanks for the idea. Unfortunately, I cannot easily modify the external project right now. Hence the reason I'm trying to just pull in a few of the source files into my project.

wpd gravatar image wpd  ( 2014-08-07 07:35:37 -0500 )edit

This is a good approach for most libraries, because they usually do support pkg-config.

joq gravatar image joq  ( 2014-08-13 11:24:34 -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

2 followers

Stats

Asked: 2014-08-06 18:11:18 -0500

Seen: 1,135 times

Last updated: Aug 07 '14