yuqli
yuqli

Reputation: 5161

ROS C++ project "include" folder?

I feel I don't understand how to set up an "include" directory in a C++ project for header files, and also how to include these headers in the .cpp files.

I'm talking about a project in the context of a ROS package, but I don't think ROS would make the structure of the project any different.

To be specific, this is the structure of the directory I'm talking about:

yuqiong@yuqiong-G7-7588:/media/yuqiong/DATA/sdd_vio$ tree
.
├── CMakeLists.txt
├── CMakeModules
│   └── FindEigen.cmake
├── config
│   ├── camchain-imucam-euroc.yaml
│   ├── camchain-imucam-fla1_11052016.yaml
│   ├── camchain-imucam-snapdragon.yaml
│   ├── custom_rosconsole.conf
│   ├── disp_vo_param.yaml
│   ├── ukf_params.yaml
│   └── vo_param.yaml
├── include
│   └── sdd_vio
│       ├── grid.h
│       ├── pinhole_camera_stereo.h
│       ├── sdd_vio_nodelet.h
│       ├── utils
│       │   ├── calib_utils.h
│       │   ├── math_utils.h
│       │   ├── ros_params_helper.h
│       │   └── timer.hpp
│       ├── visualization.h
│       └── vo_stereo.h
├── launch
│   ├── bag_reader.launch
│   ├── disp_nodelet.launch
│   ├── image_proc_rectify.launch
│   ├── imu.launch
│   ├── vins_node.launch
│   ├── vins_nodelet_euroc.launch
│   ├── vins_nodelet.launch
│   ├── vins_robot.launch
│   └── visualization.launch
├── LICENSE
├── nodelet_plugins.xml
├── package.xml
├── readme.md
├── rviz
│   ├── rviz_config_gs.rviz
│   └── rviz_config.rviz
├── src
│   ├── grid.cpp
│   ├── imu_integration_nodelet.cpp
│   ├── pinhole_camera_stereo.cpp
│   ├── sdd_vio_bag_reader.cpp
│   ├── sdd_vio_node.cpp
│   ├── sdd_vio_nodelet.cpp
│   ├── utils
│   │   ├── calib_utils.cpp
│   │   └── math_utils.cpp
│   ├── visualization.cpp
│   ├── vo_stereo_1.cpp
│   ├── vo_stereo_2.cpp
│   ├── vo_stereo_3.cpp
│   └── vo_stereo_4.cpp
└── statistics

9 directories, 47 files

So we see all the header files in the ./include/sdd_vio folder.

But in the ./src/vo_stereo_1.cpp file, it includes a header file like this:

#include "sdd_vio/vo_stereo.h"

I feel it should use this instead:

#include "../include/sdd_vio/vo_stereo.h"

Why does it omit the ../include part?

Also, it would be good if there are good resources to help me learn about build / CMake / how to figure out the correct include path in general. I've read the official CMake tutorial, some tutorials on how compile + link actually work, but still feel my knowledge is not systematic... Thanks!

Edit Adding the CMakeLists.txt file below as mentioned in the answer.

cmake_minimum_required(VERSION 2.8.3)
project(sdd_vio)

IF(DEFINED ENV{ARM_ARCHITECTURE})
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -march=native -mfpu=neon")
  #set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -Ofast -fno-signed-zeros -fno-math-errno -funroll-loops -fno-strict-aliasing")
ELSE()
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -std=c++11")
ENDIF()

# set default build type
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()


find_package(catkin REQUIRED COMPONENTS
  roscpp
  image_transport
  sensor_msgs
  cv_bridge
  visualization_msgs
  tf
  nodelet
  rosbag
)

FIND_PACKAGE(OpenCV 3 REQUIRED)
FIND_PACKAGE(Eigen3 REQUIRED)
find_package(Boost REQUIRED COMPONENTS thread)
#find_package(Ceres REQUIRED)


# Check if OpenCV package has been found
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Check if Eigen package has been found
message(STATUS "Eigen library status:")
message(STATUS "    version: ${EIGEN3_VERSION}")
message(STATUS "    libraries: ${EIGEN3_LIBS}")
message(STATUS "    include path: ${EIGEN3_INCLUDE_DIRS}")

catkin_package(
  LIBRARIES vo_nodelet imu_integration_nodelet
  DEPENDS EIGEN3 OpenCV
  CATKIN_DEPENDS roscpp image_transport sensor_msgs visualization_msgs tf nodelet
  LIBRARIES sdd_vio_nodelet
)

INCLUDE_DIRECTORIES(
  include
  ${EIGEN3_INCLUDE_DIR}
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

add_library(pinhole_camera src/pinhole_camera_stereo.cpp)
target_link_libraries(pinhole_camera ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${OpenCV_LIBS} utils)

add_library(utils src/utils/math_utils.cpp src/utils/calib_utils.cpp)
target_link_libraries(utils ${OpenCV_LIBS} ${catkin_LIBRARIES})

add_library(vo src/vo_stereo_1.cpp src/vo_stereo_2.cpp src/vo_stereo_3.cpp src/vo_stereo_4.cpp src/grid.cpp)
# add_library(vo src/disp_vo_stereo.cpp src/grid.cpp)
target_link_libraries(vo utils pinhole_camera ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${OpenCV_LIBS})  # ${CERES_LIBRARIES}


# for nodelet
add_library(vo_nodelet src/sdd_vio_nodelet.cpp src/visualization.cpp)
add_dependencies(vo_nodelet ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(vo_nodelet vo ${catkin_LIBRARIES} ${Boost_LIBRARIES})

add_library(imu_integration_nodelet src/imu_integration_nodelet.cpp)
add_dependencies(imu_integration_nodelet ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(imu_integration_nodelet ${catkin_LIBRARIES})


# for node
ADD_EXECUTABLE(sdd_vio_node src/sdd_vio_node.cpp src/visualization.cpp)

TARGET_LINK_LIBRARIES(
  sdd_vio_node
  pinhole_camera
  vo
  ${OpenCV_LIBS}
  ${catkin_LIBRARIES}
)


add_executable(sdd_vio_bag_reader src/sdd_vio_bag_reader.cpp)
target_link_libraries(sdd_vio_bag_reader vo_nodelet)

Upvotes: 1

Views: 8726

Answers (1)

SubMachine
SubMachine

Reputation: 507

Why does it omit the ../include part?

Your CMakeLists.txt file contains the following lines in it:

INCLUDE_DIRECTORIES(
  include
  ${EIGEN3_INCLUDE_DIR}
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

INCLUDE_DIRECTORIES tells the compiler where to look for header files. In this case one of the places is the include directory in your project, hence allows you to omit the ../include part. This is also the better form to use because using an absolute path and then moving the .cpp file that contains the include statement to some sub-folder in the future might break your code.

it would be good if there are good resources to help me learn about build / CMake

There are a couple of good books about Cmake, like Cmake CookBook. But in this case since you are dealing with ROS, I would suggest you start with ROS tutorials, they cover some of the basics of what you wish to know.

Upvotes: 4

Related Questions