Linking Classes in 2 user defined packages
Hello.
I'm beggeing with ROS I'm studing CMakeLists.txt using diffrent approches to execute same code. I have a problem with the linking phase of compilation of two user defined packages :
pkg1 : sphero_imu_reader I've defined a Class ImuTopicReader
pkg 2 : sphero_crash_server I've defined a Class CrashServer
I want to use the Class ImuTopicReader inside CrashServer class
the files:
sphero_imu_reader :
->src/topic_reader.h ->src/topic_reader.cpp
sphero_crash_server :
->/include/sphero_crash_server/topic_reader.h ->src/topic_reader.h ->src/topic_reader.cpp
I've got these problems :
fatal error: topic_reader.h: no such file or directory
CMakeLists.txt for sphero_topic_reader :
cmake_minimum_required(VERSION 2.8.3)
project(sphero_imu_reader)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
sensor_msgs
roscpp
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES imu_topic_reader
CATKIN_DEPENDS sensor_msgs roscpp
# DEPENDS system_lib
)
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node src/topic_reader.cpp)
add_dependencies(${PROJECT_NAME}_node ${catkin_EXPORTED_TARGETS})
target_link_libraries(${PROJECT_NAME}_node
${catk
## Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
package.xml for Sphero_imu_reader
<buildtool_depend>catkin</buildtool_depend>
<build_depend>sensor_msgs</build_depend>
<build_depend>roscpp</build_depend>
<build_export_depend>sensor_msgs</build_export_depend>
<build_export_depend>roscpp</build_export_depend>
<exec_depend>sensor_msgs</exec_depend>
<exec_depend>roscpp</exec_depend>
topic_reader.h for sphero_imu_reader pkg
#ifndef SPHERO_IMU_READER
#define SPHERO_IMU_READER
#include "ros/ros.h"
#include "sensor_msgs/Imu.h"
#endif
class ImuTopicReader
{
private:
std::string name_;
ros::NodeHandle n_;
ros::Subscriber subs_;
sensor_msgs::Imu imu_;
public:
ImuTopicReader(std::string name)
{
name_ = name;
subs_ = n_.subscribe(name_,1,&ImuTopicReader::imuCallback,this);
}
void imuCallback(const sensor_msgs::Imu::ConstPtr& msg);
sensor_msgs::Imu getImu();
};
CMakeLists.txt for sphero_crash_server :
cmake_minimum_required(VERSION 2.8.3)
project(sphero_crash_server)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
sensor_msgs
sphero_imu_reader
)
catkin_package(
CATKIN_DEPENDS roscpp sphero_imu_reader sensor_msgs
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node src/server.cpp)
add_dependencies(${PROJECT_NAME}_node } ${catkin_EXPORTED_TARGETS} ${catkin_LIBRARIES})
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
package.xml for sphero_crash_server :
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>sphero_imu_reader</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>sphero_imu_reader</build_export_depend>
<build_export_depend>sensor_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>sphero_imu_reader</exec_depend>
<exec_depend>sensor_msgs</exec_depend>
server.h in pgk sphero_crash_server :
#include "ros/ros.h"
#include "sphero_crash_server/topic_reader.h"
#include "std_srvs/Trigger.h"
#include "sensor_msgs/Imu.h"
#include <algorithm>
#include <vector>
class CrashServer
{
private:
ros::NodeHandle n_;
ImuTopicReader imu_topic_;
float threshold_;
ros::ServiceServer service_;
std::string name_;
public:
CrashServer (std::string name,float threshold)
{
name_ = name;
imu_topic_ = ImuTopicReader(name_);
threshold_ = threshold;
service_ = n_.advertiseService("detect_crash_action_serv",&CrashServer::detectionCallback,this);
std::cout << "done" <<std::endl;
}
std::string sectorDetection();
bool detectionCallback(std_srvs::Trigger::Request &req , std_srvs::Trigger::Response &res);
};