ros::package::getPath使用debug

最近有一个需求是cpp中快速构建ros的工作空间的目录,使用绝对路径肯定是可取的.发现有一个ros:📦:getPath可以使用.
了解到他是roscpp的一个函数,所以是打算直接添加头文件:

#include <ros/package.h>

然后就直接去使用:

// 获取 lio_sam 功能包的路径并构建 map 文件夹路径
string packagePath = ros::package::getPath("lio_sam");
if (packagePath.empty()) {
    cout << "Error: Could not find package 'lio_sam'" << endl;
    res.success = false;
    return false;
}
saveMapDirectory = packagePath + "/map";

但是在编译时始终出现问题:
编译出错:

/usr/bin/ld: CMakeFiles/lio_sam_mapOptmization.dir/src/mapOptmization.cpp.o: in function `mapOptimization::saveMapService(lio_sam::save_mapRequest_<std::allocator<void> >&, lio_sam::save_mapResponse_<std::allocator<void> >&)':
/home/cyun/Documents/formular_ws/src/Chapter_4/slam/lio_sam/src/mapOptmization.cpp:353: undefined reference to `ros::package::getPath(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [Chapter_4/slam/lio_sam/CMakeFiles/lio_sam_mapOptmization.dir/build.make:436/home/cyun/Documents/formular_ws/devel/lib/lio_sam/lio_sam_mapOptmization] 错误 1
make[1]: *** [CMakeFiles/Makefile2:5800:Chapter_4/slam/lio_sam/CMakeFiles/lio_sam_mapOptmization.dir/all] 错误 2
make: *** [Makefile:146:all] 错误 2
Invoking "make -j32 -l32" failed

这里我以为是roscpp没有链接到,但是查询资料发现,roscpp应该是在

${catkin_LIBRARIES}中的,而我的cmakelists.txt也是包含这一项的.
add_executable(${PROJECT_NAME}_mapOptmization src/mapOptmization.cpp)
add_dependencies(${PROJECT_NAME}_mapOptmization ${catkin_EXPORTED_TARGETS} ${PROJECT_NAME}_generate_messages_cpp)
target_compile_options(${PROJECT_NAME}_mapOptmization PRIVATE ${OpenMP_CXX_FLAGS})
target_link_libraries(${PROJECT_NAME}_mapOptmization Boost::timer ${catkin_LIBRARIES} ${PCL_LIBRARIES} ${OpenCV_LIBRARIES} ${OpenMP_CXX_FLAGS} gtsam)

后面查了getPath函数的用法后发现,不仅在roscpp中有,还需要一个roslib:

find_package(catkin REQUIRED COMPONENTS
  tf
  roscpp
  rospy
  roslib # 添加一下
  cv_bridge
  # pcl library
  pcl_conversions
  # msgs
  std_msgs
  sensor_msgs
  geometry_msgs
  nav_msgs
  message_generation
  visualization_msgs
)

然后对应的package.xml添加即可:

  <build_depend>roscpp</build_depend>
  <build_depend>roslib</build_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>roslib</run_depend>

正常编译:
在这里插入图片描述

OF/********************************************************************************************************************* Copyright (c) 2020 RoboSense All rights reserved By downloading, copying, installing or using the software you agree to this license. If you do not agree to this license, do not download, install, copy or use the software. License Agreement For RoboSense LiDAR SDK Library (3-clause BSD License) Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the names of the RoboSense, nor Suteng Innovation Technology, nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *********************************************************************************************************************/ #include "manager/node_manager.hpp" #include <rs_driver/macro/version.hpp> #include <signal.h> #ifdef ROS_FOUND #include <ros/ros.h> #include <ros/package.h> #elif ROS2_FOUND #include <rclcpp/rclcpp.hpp> #endif using namespace robosense::lidar; #ifdef ROS2_FOUND std::mutex g_mtx; std::condition_variable g_cv; #endif static void sigHandler(int sig) { RS_MSG << "RoboSense-LiDAR-Driver is stopping....." << RS_REND; #ifdef ROS_FOUND ros::shutdown(); #elif ROS2_FOUND g_cv.notify_all(); #endif } /////////////////////////////////////////////////////////////////////////////////////////////// static std::string pcd_name_prefix = ""; void SetPCDNamePrefix(const std::string& prefix) { RS_INFO << "------------------------------------------------------" << RS_REND; RS_INFO << "Set PCD Name Prefix: " << prefix << RS_REND; pcd_name_prefix = prefix; RS_INFO << "------------------------------------------------------" << RS_REND; } const std::string& GetPCDNamePrefix() { // RS_INFO << "Use PCD Name Prefix: " << pcd_name_prefix << RS_REND; return pcd_name_prefix; } /////////////////////////////////////////////////////////////////////////////////////////////// static std::string pcd_file_path = ""; void SetPCDFilePath(const std::string& path) { RS_INFO << "------------------------------------------------------" << RS_REND; RS_INFO << "Set PCD Dir: " << path << RS_REND; pcd_file_path = path; RS_INFO << "------------------------------------------------------" << RS_REND; } const std::string& GetPCDFilePath() { // RS_INFO << "Use PCD Dir: " << pcd_file_path << RS_REND; return pcd_file_path; } /////////////////////////////////////////////////////////////////////////////////////////////// void StringSplit(const std::string& str, const char split_char, std::vector<std::string>& res) { std::istringstream iss(str); std::string token; while(getline(iss, token, split_char)) { res.push_back(std::move(token)); } } const std::size_t LIDAR_INDEX_IN_PCAP_FILE = 4; /////////////////////////////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { signal(SIGINT, sigHandler); ///< bind ctrl+c signal with the sigHandler function RS_TITLE << "********************************************************" << RS_REND; RS_TITLE << "********** **********" << RS_REND; RS_TITLE << "********** RSLidar_SDK Version: v" << RSLIDAR_VERSION_MAJOR << "." << RSLIDAR_VERSION_MINOR << "." << RSLIDAR_VERSION_PATCH << " **********" << RS_REND; RS_TITLE << "********** **********" << RS_REND; RS_TITLE << "********************************************************" << RS_REND; #ifdef ROS_FOUND ros::init(argc, argv, "rslidar_sdk_node", ros::init_options::NoSigintHandler); #elif ROS2_FOUND rclcpp::init(argc, argv); #endif std::string config_path; #ifdef RUN_IN_ROS_WORKSPACE config_path = ros::package::getPath("rslidar_sdk"); #else config_path = (std::string)PROJECT_PATH; #endif config_path += "/config/"; #ifdef ROS_FOUND ros::NodeHandle priv_hh("~"); std::string pcap_file; std::string dest_pcd_dir; priv_hh.param("pcap_file", pcap_file, std::string("")); priv_hh.param("dest_pcd_dir", dest_pcd_dir, std::string("")); #elif ROS2_FOUND std::shared_ptr<rclcpp::Node> nd = rclcpp::Node::make_shared("param_handle"); std::string pcap_file = nd->declare_parameter<std::string>("pcap_file", ""); std::string dest_pcd_dir = nd->declare_parameter<std::string>("dest_pcd_dir", ""); #endif // #if defined(ROS_FOUND) || defined(ROS2_FOUND) // if (!path.empty()) // { // config_path = path; // } // #endif std::string lidar_name = ""; std::vector<std::string> path_nodes{}; StringSplit(pcap_file, '/', path_nodes); std::vector<std::string> filename_segs{}; std::vector<std::string> token_res{}; if (path_nodes.size() > 0) { StringSplit(path_nodes[path_nodes.size()-1], '.', filename_segs); if (filename_segs.size() > 0) { StringSplit(filename_segs[0], '_', token_res); } else { RS_ERROR << "The lidar name in pcap file is not specified: [" << pcap_file << "] is wrong. Please check." << RS_REND; return -1; } } if (token_res.size() <= LIDAR_INDEX_IN_PCAP_FILE) { RS_ERROR << "The lidar name in pcap file is not specified: [" << pcap_file << "] is wrong. Please check." << RS_REND; return -1; } else { lidar_name = token_res[LIDAR_INDEX_IN_PCAP_FILE]; std::stringstream ss; for (std::size_t i = 0; i <= LIDAR_INDEX_IN_PCAP_FILE; i++) { ss << token_res[i] << "_"; } SetPCDNamePrefix(ss.str()); } std::stringstream ss; ss << "config-" << lidar_name << ".yaml"; config_path = config_path + ss.str(); if (dest_pcd_dir.empty()) { RS_ERROR << "The destination pcd output dir is not specified: [" << dest_pcd_dir << "] is wrong. Please check." << RS_REND; return -1; } else { SetPCDFilePath(dest_pcd_dir); } SourceDriverPCAPPath::SetPCAPFilePath(pcap_file); YAML::Node config; try { config = YAML::LoadFile(config_path); RS_INFO << "--------------------------------------------------------" << RS_REND; RS_INFO << "Config loaded from PATH:" << RS_REND; RS_INFO << config_path << RS_REND; RS_INFO << "--------------------------------------------------------" << RS_REND; } catch (...) { RS_ERROR << "The format of config file " << config_path << " is wrong. Please check (e.g. indentation)." << RS_REND; return -1; } std::shared_ptr<NodeManager> demo_ptr = std::make_shared<NodeManager>(); demo_ptr->init(config); demo_ptr->start(); RS_MSG << "RoboSense-LiDAR-Driver is running....." << RS_REND; #ifdef ROS_FOUND ros::spin(); #elif ROS2_FOUND std::unique_lock<std::mutex> lck(g_mtx); g_cv.wait(lck); #endif return 0; }
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白云千载尽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值