ROS C ++项目的“ include”文件夹?

时间:2019-07-19 22:45:58

标签: c++ build ros

我觉得我不明白如何在C ++项目中为头文件设置“ include”目录,以及如何在.cpp文件中包含这些头。

我是在ROS包的上下文中谈论一个项目,但我认为ROS不会使项目的结构有所不同。

具体来说,这是我正在谈论的目录结构:

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

因此,我们在./include/sdd_vio文件夹中看到了所有头文件。

但是在./src/vo_stereo_1.cpp文件中,它包含这样的头文件:

#include "sdd_vio/vo_stereo.h"

我觉得应该改用它:

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

为什么省略了../include部分?

如果有足够的资源来帮助我了解build / CMake /通常如何找出正确的包含路径,那也将是一件好事。我已经阅读了官方的CMake教程,一些关于编译+链接实际上如何工作的教程,但是仍然觉得我的知识不是系统性的...谢谢!

修改 如答案中所述,添加下面的CMakeLists.txt文件。

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)

1 个答案:

答案 0 :(得分:3)

  

为什么省略../include部分?

您的CMakeLists.txt文件中包含以下几行:

<Contact.Name {...contact}>
<Organization.Contact.Name  {...organization}>
<ScopedOrganization.Organization.Contact.Name {...scopedOrgniazation}>

INCLUDE_DIRECTORIES( include ${EIGEN3_INCLUDE_DIR} ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ) tells the compiler where to look for header files。在这种情况下,位置之一是项目中的INCLUDE_DIRECTORIES目录,因此可以省略include部分。这也是更好的使用形式,因为将来使用绝对路径,然后将包含include语句的.cpp文件移动到某个子文件夹,可能会破坏您的代码。

  

如果有足够的资源来帮助我了解build / CMake,那就太好了

有一些关于Cmake的好书,例如Cmake CookBook。但是在这种情况下,由于您正在处理ROS,因此建议您从ROS tutorials开始,它们涵盖了您希望了解的一些基本知识。