使用QTCreater 开发ros程序(qmake 编译 并且不需要ros_qt 插件)
前言 ,ROS 默认使用 catkin_make 作为编译器,catkin_make 是cmake 的变种。当项目需要Qt开发界面(qmake编译),又需要作为ROS节点,使用QtCreater作为开发工具
核心步骤2个:
1 、需要把ros安装目录下的include 头文件目录添加到包含搜索目录
所以在 pro 文件中加入
#--add ros include
INCLUDEPATH += -I /opt/ros/kinetic/include
DEPENDPATH += /opt/ros/kinetic/include
2 、需要把ros程序依赖的库链接到程序中
所以在 pro 文件中加入
#--add ros libs
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lroscpp
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lroslib
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lpthread
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lroscpp_serialization
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrostime
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrosconsole
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrosconsole_log4cxx
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrosconsole_backend_interface
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lxmlrpcpp
完整代码
rosNode.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = rosNode
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
#--add ros include
INCLUDEPATH += -I /opt/ros/kinetic/include
DEPENDPATH += /opt/ros/kinetic/include
#--add ros libs
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lroscpp
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lroslib
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lpthread
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lroscpp_serialization
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrostime
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrosconsole
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrosconsole_log4cxx
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lrosconsole_backend_interface
unix:!macx: LIBS += -L /opt/ros/kinetic/lib/ -lxmlrpcpp
main.cpp
#include <QCoreApplication>
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
/**
* This is a message object. You stuff it with data, and then publish it.
*/
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
/**
* The publish() function is how you send messages. The parameter
* is the message object. The type of this object must agree with the type
* given as a template parameter to the advertise<>() call, as was done
* in the constructor above.
*/
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return a.exec();
}