set(GOOGLE_PROTOBUF_DIR ${PROJECT_SOURCE_DIR}/protobuf)
时间: 2023-12-18 15:02:40 浏览: 133
这段代码是设置变量 `GOOGLE_PROTOBUF_DIR` 的值为 `${PROJECT_SOURCE_DIR}/protobuf`,其中 `${PROJECT_SOURCE_DIR}` 是 CMake 中的一个内置变量,表示当前 CMakeLists.txt 所在的目录。这段代码的作用可能是为了指定 Google Protocol Buffers 库的安装路径或源码路径。
相关问题
在ros项目中添加发送websocket wss消息的功能,修改如下代码并在CmakeLists.txt中添加依赖,实现将serialized_data发送到wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840。main.cpp:#include "ros/ros.h" #include "std_msgs/String.h" #include <boost/thread/locks.hpp> #include <boost/thread/shared_mutex.hpp> #include "third_party/apollo/proto/perception/perception_obstacle.pb.h" #include "t3_perception.pb.h" apollo::perception::PerceptionObstacles perception_obstacles_; void perceptionCallback(const std_msgs::String& msg) { ROS_WARN("t3 perceptionCallback parse"); if (perception_obstacles_.ParseFromString(msg.data)) { double timestamp = perception_obstacles_.header().timestamp_sec(); ROS_INFO("t3 perceptionCallback timestamp %f count:%d", timestamp, perception_obstacles_.perception_obstacle().size()); std::string data; perception_obstacles_.SerializeToString(&data); VehData veh_data; veh_data.set_messagetype(5); veh_data.set_messagedes("PerceptionObstacles"); veh_data.set_contents(data); std::string serialized_data; veh_data.SerializeToString(&serialized_data); } else { ROS_ERROR("t3 perceptionCallback parse fail!"); } } int main(int argc, char **argv) { ros::init(argc, argv, "listener"); ros::NodeHandle n; ros::Subscriber sub = n.subscribe("/perception_node/perception_objects", 1000, perceptionCallback); ros::spin(); return 0; }CMakeLists.txt:cmake_minimum_required(VERSION 3.0.2) project(t3) find_package(catkin REQUIRED COMPONENTS roscpp rospy pcl_ros std_msgs third_party ) find_package(Protobuf REQUIRED) include_directories(${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR}/..) find_package(Boost REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) set(ixwebsocket_INCLUDE_DIR "/usr/local/include/ixwebsocket") set(ixwebsocket_LIBRARIES "/usr/local/lib/libixwebsocket.a") include_directories(${ixwebsocket_INCLUDE_DIR}) include_directories(${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}/${PROJECT_NAME}) include_directories(${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_INCLUDE_DESTINATION}/smartview) catkin_package(INCLUDE_DIRS ${PROJECT_INCLUDE_DIRS} DEPENDS ${GFLAGS_LIBRARIES} ) include_directories( ${catkin_INCLUDE_DIRS} ${PROTOBUF_INCLUDE_DIR} ${PROJECT_SOURCE_DIR}/.. ) add_executable(${PROJECT_NAME}_node src/main.cpp ) add_dependencies(${PROJECT_NAME}_node ${catkin_EXPORTED_TARGETS}) target_link_libraries(${PROJECT_NAME}_node ${catkin_LIBRARIES} ${PROTOBUF_LIBRARIES} smartview_proto ) install(TARGETS ${PROJECT_NAME}_node ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} )
你可以使用以下代码来添加发送websocket wss消息的功能,同时在CmakeLists.txt中添加依赖:
```cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
#include "third_party/apollo/proto/perception/perception_obstacle.pb.h"
#include "t3_perception.pb.h"
#include <ixwebsocket/IXWebSocket.h>
using namespace ix;
apollo::perception::PerceptionObstacles perception_obstacles_;
void perceptionCallback(const std_msgs::String& msg) {
ROS_WARN("t3 perceptionCallback parse");
if (perception_obstacles_.ParseFromString(msg.data)) {
double timestamp = perception_obstacles_.header().timestamp_sec();
ROS_INFO("t3 perceptionCallback timestamp %f count:%d", timestamp, perception_obstacles_.perception_obstacle().size());
std::string data;
perception_obstacles_.SerializeToString(&data);
VehData veh_data;
veh_data.set_messagetype(5);
veh_data.set_messagedes("PerceptionObstacles");
veh_data.set_contents(data);
std::string serialized_data;
veh_data.SerializeToString(&serialized_data);
// create a WebSocket connection
WebSocket websocket;
websocket.setTLSOptions({}); // use default options
websocket.setOnMessageCallback([](const WebSocketMessagePtr& msg) {
std::cout << "Received message: " << msg->str << std::endl;
});
websocket.setOnCloseCallback([](WebSocket& ws, const WebSocketCloseInfo& info) {
std::cout << "WebSocket closed: " << info.reason << std::endl;
});
websocket.setOnErrorCallback([](const std::error_code& ec) {
std::cout << "Error: " << ec << ", error message: " << ec.message() << std::endl;
});
websocket.connect("wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840");
websocket.send(serialized_data);
websocket.close();
} else {
ROS_ERROR("t3 perceptionCallback parse fail!");
}
}
int main(int argc, char **argv) {
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/perception_node/perception_objects", 1000, perceptionCallback);
ros::spin();
return 0;
}
```
在CmakeLists.txt中添加以下依赖:
```
find_package(ixwebsocket REQUIRED)
target_link_libraries(${PROJECT_NAME}_node ixwebsocket)
```
注意,你需要安装ixwebsocket库以使用WebSocket功能。
MACRO(GENPROTO) FILE(GLOB PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.proto) FOREACH(proto ${PROTO_FILES}) FILE(TO_NATIVE_PATH ${proto} proto_native) string(REPLACE ".proto" ".pb.cc" PbCC ${proto_native}) string(REPLACE ".proto" ".pb.h" Pbh ${proto_native}) string(REPLACE "${PROJECT_SOURCE_DIR}/" "${PROJECT_BINARY_DIR}/" PbCC ${PbCC}) string(REPLACE "${PROJECT_SOURCE_DIR}/" "${PROJECT_BINARY_DIR}/" Pbh ${Pbh}) if ((not EXISTS ${PbCC}) OR (${proto_native} IS_NEWER_THAN ${PbCC})) # message("proto: " ${proto_native} " -> " ${PbCC}) EXECUTE_PROCESS(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${PROJECT_SOURCE_DIR} --cpp_out=${PROJECT_BINARY_DIR} ${proto_native} RESULT_VARIABLE res_var ) if (res_var STREQUAL "1") message(FATAL_ERROR "Generate proto error!!! file: ${proto_native}") endif() execute_process(COMMAND cp ${Pbh} ${CMAKE_CURRENT_SOURCE_DIR} ERROR_VARIABLE res_var) if(res_var) message(FATAL_ERROR ${res_var}) endif() endif() ENDFOREACH(proto) ENDMACRO()
这是一个CMake脚本中的宏定义,用于生成Protocol Buffer的C++代码。具体来说,该宏会遍历当前目录下的所有`.proto`文件,使用`PROTOBUF_PROTOC_EXECUTABLE`指定的编译器生成对应的`.pb.cc`和`.pb.h`文件,并将这些文件拷贝到源代码目录中。其中,`PROTOBUF_PROTOC_EXECUTABLE`是一个环境变量,用于指定Protocol Buffer的编译器路径。该脚本的主要功能是简化生成Protocol Buffer代码的过程,方便用户在CMake项目中使用。
阅读全文