Ask Your Question
1

How do I include the header?

asked 2016-11-29 16:45:13 -0500

215 gravatar image

updated 2016-11-30 05:32:44 -0500

I am currently having some problem including header within my catkin_package.

This is the file structure:

pkg_a/
├── CMakeLists.txt
├── CMakeLists.txt.user
├── include
│   └── pkg_a
│       ├── pugiconfig.hpp
│       └── pugixml.hpp
├── package.xml
├── src
     ├── src.cpp
     ├── header_to_src.h
     ├── node.cpp
     ├── pugixml.cpp
     ├── src_2.cpp
     └── header_to_src_2.h

I am trying to include pugixml.hpp in the cpp. file. The way i've tried to do it is like this

 #include <pkg_a/pugixml.hpp>

But it cannot find it.. What is the path to it.. I would like to avoid making direct path to the include folder, such that other packages also easily can enter include the .hpp file.

This is the CMakeList

cmake_minimum_required(VERSION 2.8.3)
project(pkg_a)

#Compiler flags
add_definitions(-std=c++1y)
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  genmsg
  message_generation
  depend_1
  depend_2
)


catkin_package(
   INCLUDE_DIRS include
   LIBRARIES ${PROJECT_NAME}
   CATKIN_DEPENDS
   DEPENDS system_lib

)

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

## Mark cpp header files for installation
 install(DIRECTORY include/${PROJECT_NAME}/
   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
   FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
   PATTERN ".svn" EXCLUDE
 )

## Mark other files for installation (e.g. launch and bag files, etc.)
 install(
     DIRECTORY include/${PROJECT_NAME}/
#   # myfile1
#   # myfile2
   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
 )

SET(SRC_FILES
        ${SRC_FILES}
        ${CMAKE_CURRENT_SOURCE_DIR}/src/src.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src/src_2.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src/pugixml.cpp
#       ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
        CACHE INTERNAL ""
)

add_library(${PROJECT_NAME} ${SRC_FILES})

add_executable(run_${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/node.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/pugixml.cpp)
add_dependencies( ${PROJECT_NAME} depend_1_generate_messages_cpp depend_2_generate_message_cpp )
target_link_libraries(run_${PROJECT_NAME}
    ${PROJECT_NAME}
    ${catkin_LIBRARIES}
)
edit retag flag offensive close merge delete

Comments

Probably unrelated, but:

add_executable(run_${PROJECT_NAME}
...
add_dependencies( ${PROJECT_NAME} ..)

That should probably be run_${PROJECT_NAME} in both cases.

gvdhoorn gravatar imagegvdhoorn ( 2016-11-30 05:54:40 -0500 )edit

Also:

set (SRC_FILES
   ${SRC_FILES}
   ${CMAKE_CURRENT_SOURCE_DIR}/src/..
   ..
)

The first entry recurses on itself (var is empty at that point most likely), and the ${CMAKE_CURRENT_SOURCE_DIR} are unnecessary: the paths are already relative to the current file.

gvdhoorn gravatar imagegvdhoorn ( 2016-11-30 05:56:10 -0500 )edit

Also:

CACHE INTERNAL ""

why?

gvdhoorn gravatar imagegvdhoorn ( 2016-11-30 05:58:05 -0500 )edit

changing add_dependecies to run_ mess up with the message generation of depend_1 and depend_2.. So that could not be it.

215 gravatar image215 ( 2016-11-30 06:12:45 -0500 )edit

About the recurses.. it should be a problem. I mean it checks itself, and then the next.

215 gravatar image215 ( 2016-11-30 06:12:50 -0500 )edit

about cache internal.. to be sure i don't know. I removed it, it still works as before

215 gravatar image215 ( 2016-11-30 06:13:09 -0500 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2016-11-30 05:13:48 -0500

gvdhoorn gravatar image

This is more a generic CMake question than really ROS specific (ie: setting up your include path is not something that is any different whether you do it for a ROS node or for a non-ROS application / build target).

In the general case, in your CMakeLists.txt:

include_directories(/path/to/the/include/dir)

For a ROS package, you probably want the include sub directory of your package to be on the include path, so:

# note: this path is relative to the current CMakeLists.txt
include_directories(include)

If you have used find_package(catkin COMPONENTS ..), you probably want something like:

include_directories(include ${catkin_INCLUDE_DIRS})

The x_INCLUDE_DIRS (where x is a package name) is a general pattern: most CMake Find scripts will set these variables for you, but do check the documentation.

See How to do common tasks » Package format 2 (recommended) » C++ system library dependencies for some more info on resolving and using system dependencies with Catkin (but again, really: CMake).

edit flag offensive delete link more

Comments

Well... I did follow that guide you are linking to. The weird part is that the package compile if i run catkin_make --only-pkg-with-deps but that only compiles that packages with its deps.

215 gravatar image215 ( 2016-11-30 05:35:29 -0500 )edit

But if i compile it using catkin_make it will not work. It comes up with the error, that it cannot find the .hpp file.

215 gravatar image215 ( 2016-11-30 05:36:22 -0500 )edit
1

I think we'll need a minimal example to reproduce this. Somewhere you are (not) doing something that causes this error.

Is the error in the package that provides the header (ie: pkg_a), or does a dependent pkg error?

gvdhoorn gravatar imagegvdhoorn ( 2016-11-30 05:59:24 -0500 )edit

oh wait.. I think you are right @gvdhoorn it looks like, it was looking for the header file when it was building a different package. They only share service message, which are generated by depend_1 and depend_2.

215 gravatar image215 ( 2016-11-30 06:29:25 -0500 )edit

but for some reason was it also looking for the pugixml.hpp, which makes sense it fails as it is not depending of the package that uses it. So i added pkg_a to the dependency list of the the other package, and that seemed to have fixed it. I am still confused on why it is looking for the header.

215 gravatar image215 ( 2016-11-30 06:30:58 -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-11-29 16:45:13 -0500

Seen: 8,223 times

Last updated: Nov 30 '16