引自:https://blog.csdn.net/wawayu_0/article/details/79410237
1. 如何引用自定義頭文件
• 引用當前軟件包內的頭文件
在包的目錄include下建test_pack.h文件
#ifndef _TEST_PKG_
#define _TEST_PKG_
#define TEST_PKG_VER "1.0.0"
#define INIT_COUNT 100
int addTwoNum(int a,int b);
#endif
修改CMakeLists.txt參數
include_directories(
include
${catkin_INCLUDE_DIRS}
)
2.引用別的包的頭文件
catkin_package(
INCLUDE_DIRS include
# LIBRARIES test
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
將包發布,才能使用。
3.引用動態鏈接庫.so文件
mkdir lib
mkdir include/my_pkg
cd include/my_pkg
nano multiply.h
#ifndef _MULTIPLY_H_
#define _MULTIPLY_H_
#ifdef __cpluscplus
extern "c"
{
#endif
int multiply(int a,int b);
#ifdef __cpluscplus
#endif
#endif
nano multiply.cpp
#include "multiply.h"
int multiply(int a,int b)
{
return a*b;
}
nano my_pkg/src/my_pkg.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include "test/test_pkg.h"
#include "my_pkg/multiply.h"
int main(int argc, char **argv)
{
ros::init(argc, argv, "example_talker_msg");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::String>("message", 1000);
ros::Rate loop_rate(10);
int count=multiply(30,5);
ROS_INFO("test version:%s,init count:%d",TEST_PKG_VER,INIT_COUNT);
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "my_pkg" << count;
修改CMakeLists.txt
include_directories(
include
${catkin_INCLUDE_DIRS}
)
link_directories(
lib
${catkin_LIB_DIRS}
)
##
編譯
運行
以上確實解決了我的部分問題,但是仍然沒有解決我的ROS程序不能引用自定義消息包頭文件的問題。