Rosanswers logo

I'm trying to do the same as this example : https://answers.ros.org/question/39523/rospackagegetpath-problem/

The function is documented here : http://docs.ros.org/en/jade/api/roslib/html/c++/namespaceros_1_1package.html

I created a minimum reproductible example of my problem, which I tested both on Kinetic and Noetic :

test.cpp :

#include <iostream>
#include <ros/package.h>

int main(int argc, char **argv)
   std::cout << "Hello, world!" << std::endl;
   std::string testString = ros::package::getPath("testPath");
   std::cout << "Package path is "  << testString << std::endl;

Package.xml :

<?xml version="1.0"?>
<package format="2">
<description>The testPath package</description>

<maintainer email="[email protected]">victor</maintainer>



  <!-- Other tools can request additional information be placed here -->


CmakeList :

cmake_minimum_required(VERSION 3.0.2)

find_package(catkin REQUIRED COMPONENTS
#  INCLUDE_DIRS include
#  LIBRARIES testPath
#  CATKIN_DEPENDS roscpp roslib
#  DEPENDS system_lib

# include

## Declare a C++ library
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/testPath.cpp
# )

add_executable(${PROJECT_NAME}_node src/test.cpp)

The linking error I'm getting :

undefined reference to « ros::package::getPath(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) »

Any leads on how to make this work?

Originally posted by Vic on ROS Answers with karma: 71 on 2022-05-03

Post score: 1


1 Answer 1


Rosanswers logo

Where do you call target_link_libraries(... ${catkin_LIBRARIES}) in your CMakeLists.txt?

Originally posted by gvdhoorn with karma: 86574 on 2022-05-03

This answer was ACCEPTED on the original site

Post score: 3

Original comments

Comment by Vic on 2022-05-04:
You were right. Adding


in the CmakeLists.txt fixed the issue. I expected it to be done automatically when created the package. Thanks

Comment by Vic on 2022-05-04:
As @gvdhoorn pointed, I was missing the target_link_libraries in CmakeList.txt

Comment by Joe28965 on 2022-05-04:
If gvdhoorn answered your question, please accept it as an answer. Thanks in advance


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.