微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有 ROS 包的 CMake:未检测到头文件

如何解决带有 ROS 包的 CMake:未检测到头文件

我试图将我的代码组织成更面向 OOP 的风格,每个类都有自己的头文件和 cpp 文件。这是我在工作区中的包文件夹中的树

- CMakeLists.txt
- include
- - Master_Thread.h
- - Entry.h
- - ROS_Topic_Thread.h
- src
- - pcl_ros_init.cpp
- - archive
- - - pcl_ros_not_updated.cpp
- - - pc_ros_old2.cpp
- - - thread.cpp
- - - pcl_ros_old.cpp
- package.xml

我没有分别在cpp文件中放置类定义,我暂时将它们与主代码(pcl_ros_init.cpp)一起放置。我的类彼此之间有一些轻微的依赖,例如 Master_Thread 类在其声明中使用了 ROS_Topic_Thread,现在,Cmake 能够找到包含文件,但是在像 Master_Thread.h 这样的头文件中,它无法找到ROS_Topic_Thread 等 pcl_ros_custom/include/Master_Thread.h:38:36: error: ‘ROS_Topic_Thread’ has not been declared

这是我的 CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(pcl_ros_custom)

find_package(catkin required COMPONENTS
roscpp
pcl_conversions
pcl_ros)

catkin_package(
INCLUDE_Dirs
CATKIN_DEPENDS roscpp
           pcl_conversions
           pcl_ros
)

include_directories(
include
${catkin_INCLUDE_Dirs}/include/**
)

add_executable(pcl_ros_init src/pcl_ros_init.cpp)
target_link_libraries(pcl_ros_init 
            ${catkin_LIBRARIES} ${roslib_LIBRARIES} ${PCL_LIBRARIES} )

这是两个头文件

#ifndef MASTER_THREAD_H
#define MASTER_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>

#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>

//include custom classes
#include "ROS_Topic_Thread.h"
#include "ClusteringBenchmarkEntry.h"


namespace OCB
{
    class Master_Thread
{
    private:
    std::atomic<bool> mKillswitch;
    std::vector<sensor_msgs::PointCloud2ConstPtr> mMasterbuffer1,mMasterbuffer2,mMasterbuffer3,mMasterbuffer4,mMergedPointCloud2Ptr_buffer;
    public:
    Master_Thread();
    void transfer_to_master_buffer(ROS_Topic_Thread&t1,ROS_Topic_Thread&t2,ROS_Topic_Thread&t3,ROS_Topic_Thread&t4 );
    
    void process_buffer();
    void setkillswitch();
};

} 
#endif
#ifndef ROS_TOPIC_THREAD_H
#define ROS_TOPIC_THREAD_H

#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>

#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>


#include "Master_Thread.h"
#include "ClusteringBenchmarkEntry.h"

namespace OCB
{
class ROS_Topic_Thread
{
    private:
    ros::Subscriber mSub;

    // Topic
    const std::string mSUB_TOPIC;

    //buffer
    std::vector<sensor_msgs::PointCloud2ConstPtr> mThreadbuffer;
    
    std::mutex mMutex;

    public:
    ROS_Topic_Thread()= delete;
    ROS_Topic_Thread(ros::NodeHandle* nodehandle,const std::string& SUBTOPIC,ClusteringBenchmarkEntry* entry);
  
    void callback(const sensor_msgs::PointCloud2ConstPtr& sub_ptr);
    friend class Master_Thread;

};

} //namespace close scope

#endif //for ROS_TOPIC_THREAD_H

我哪里出错了?

编辑

This一个非常好的链接,它清除了我在 C++ 中的大部分头文件组织,它在答案之上帮助我使项目构建顺利进行。希望对阅读本文的人有所帮助。

解决方法

本质上,您不需要将任一标头包含在另一个标头中。这修复了通函包括。 在 ROS_Topic_Thread.h 中,friend class Master_Thread; 已经转发声明了 Mater_Thread,所以你不需要 Master_Thread.h 头文件。 在 Master_Thread.h 中,您还可以在 Master_Thread 类的声明之前转发声明 class ROS_Topic_Thread;,因为您只在 transfer_to_master_buffer 方法中使用对 ROS_Topic_Thread 的引用。

#ifndef MASTER_THREAD_H
#define MASTER_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>

#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>

//include custom classes
#include "ClusteringBenchmarkEntry.h"


namespace OCB
{
    class ROS_Topic_Thread;
    class Master_Thread
{
    private:
    std::atomic<bool> mKillswitch;
    std::vector<sensor_msgs::PointCloud2ConstPtr> mMasterbuffer1,mMasterbuffer2,mMasterbuffer3,mMasterbuffer4,mMergedPointCloud2Ptr_buffer;
    public:
    Master_Thread();
    void transfer_to_master_buffer(ROS_Topic_Thread&t1,ROS_Topic_Thread&t2,ROS_Topic_Thread&t3,ROS_Topic_Thread&t4 );
    
    void process_buffer();
    void setkillswitch();
};

} 
#endif
#ifndef ROS_TOPIC_THREAD_H
#define ROS_TOPIC_THREAD_H

#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>

#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>

#include "ClusteringBenchmarkEntry.h"

namespace OCB
{
class ROS_Topic_Thread
{
    private:
    ros::Subscriber mSub;

    // Topic
    const std::string mSUB_TOPIC;

    //buffer
    std::vector<sensor_msgs::PointCloud2ConstPtr> mThreadbuffer;
    
    std::mutex mMutex;

    public:
    ROS_Topic_Thread()= delete;
    ROS_Topic_Thread(ros::NodeHandle* nodehandle,const std::string& SUBTOPIC,ClusteringBenchmarkEntry* entry);
  
    void callback(const sensor_msgs::PointCloud2ConstPtr& sub_ptr);
    friend class Master_Thread;

};

} //namespace close scope

#endif //for ROS_TOPIC_THREAD_H

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。