How do I include the header?
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}
)
Asked by 215 on 2016-11-29 17:45:13 UTC
Answers
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).
Asked by gvdhoorn on 2016-11-30 06:13:48 UTC
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.
Asked by 215 on 2016-11-30 06:35:29 UTC
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.
Asked by 215 on 2016-11-30 06:36:22 UTC
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?
Asked by gvdhoorn on 2016-11-30 06:59:24 UTC
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.
Asked by 215 on 2016-11-30 07:29:25 UTC
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.
Asked by 215 on 2016-11-30 07:30:58 UTC
Comments
Probably unrelated, but:
That should probably be
run_${PROJECT_NAME}
in both cases.Asked by gvdhoorn on 2016-11-30 06:54:40 UTC
Also:
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.Asked by gvdhoorn on 2016-11-30 06:56:10 UTC
Also:
why?
Asked by gvdhoorn on 2016-11-30 06:58:05 UTC
changing add_dependecies to run_ mess up with the message generation of depend_1 and depend_2.. So that could not be it.
Asked by 215 on 2016-11-30 07:12:45 UTC
About the recurses.. it should be a problem. I mean it checks itself, and then the next.
Asked by 215 on 2016-11-30 07:12:50 UTC
about cache internal.. to be sure i don't know. I removed it, it still works as before
Asked by 215 on 2016-11-30 07:13:09 UTC