Ask Your Question

Calling ROS libraries from external CPP

asked 2016-11-12 23:03:16 -0500

Ravi Joshi gravatar image

updated 2016-11-13 09:19:53 -0500

I want to call the ROS libraries (shared object .so files) from external CPP program. Below are the ROS libraries, which I copied from /opt/ros/indigo/lib/ to a sub-directory-

  1. /opt/ros/indigo/lib/
  2. /opt/ros/indigo/lib/

I used g++ to compile the main code i.e., test.cpp with following command-

g++ -Iinclude -I/usr/include/eigen3 -Lshared -lorocos-kdl -lkdl_parser -std=c++11 test.cpp -o test

but it is not working, since few more dependencies need to satisfy. Below is the error -

test@test:~/Desktop/calll/calling_so$ g++ -Iinclude -I/usr/include/eigen3 -Lshared -lorocos-kdl -lkdl_parser -std=c++11 test.cpp -o test
/tmp/ccVP6thR.o: In function `main':
test.cpp:(.text+0x103): undefined reference to `KDL::Tree::Tree(std::string const&)'
test.cpp:(.text+0x13a): undefined reference to `kdl_parser::treeFromFile(std::string const&, KDL::Tree&)'
test.cpp:(.text+0x164): undefined reference to `KDL::Chain::Chain()'
test.cpp:(.text+0x188): undefined reference to `KDL::Tree::getChain(std::string const&, std::string const&, KDL::Chain&) const'
test.cpp:(.text+0x1f4): undefined reference to `KDL::ChainDynParam::ChainDynParam(KDL::Chain const&, KDL::Vector)'
test.cpp:(.text+0x216): undefined reference to `KDL::JntArray::JntArray(unsigned int)'
test.cpp:(.text+0x238): undefined reference to `KDL::JntSpaceInertiaMatrix::JntSpaceInertiaMatrix(int)'
test.cpp:(.text+0x258): undefined reference to `KDL::ChainDynParam::JntToMass(KDL::JntArray const&, KDL::JntSpaceInertiaMatrix&)'
test.cpp:(.text+0x271): undefined reference to `KDL::JntSpaceInertiaMatrix::operator()(unsigned int, unsigned int)'
test.cpp:(.text+0x2a6): undefined reference to `KDL::JntSpaceInertiaMatrix::~JntSpaceInertiaMatrix()'
test.cpp:(.text+0x2b5): undefined reference to `KDL::JntArray::~JntArray()'
test.cpp:(.text+0x2c4): undefined reference to `KDL::ChainDynParam::~ChainDynParam()'
test.cpp:(.text+0x2d3): undefined reference to `KDL::Chain::~Chain()'
test.cpp:(.text+0x39b): undefined reference to `KDL::JntSpaceInertiaMatrix::~JntSpaceInertiaMatrix()'
test.cpp:(.text+0x3af): undefined reference to `KDL::JntArray::~JntArray()'
test.cpp:(.text+0x3c3): undefined reference to `KDL::ChainDynParam::~ChainDynParam()'
test.cpp:(.text+0x3d7): undefined reference to `KDL::Chain::~Chain()'
/tmp/ccVP6thR.o: In function `KDL::TreeElement::~TreeElement()':
test.cpp:(.text._ZN3KDL11TreeElementD2Ev[_ZN3KDL11TreeElementD5Ev]+0x26): undefined reference to `KDL::Segment::~Segment()'
collect2: error: ld returned 1 exit status

Below is test.cpp-

#include <iostream>
#include <kdl/jntarray.hpp>
#include <kdl/chaindynparam.hpp>
#include <kdl_parser/kdl_parser.hpp>
#include <kdl/jntspaceinertiamatrix.hpp>

int main(int argc, char** argv)
    std::string urdf_file = "robot.urdf";
    std::string base_link = "base";
    std::string tip_link = "end_effector";

    KDL::Tree tree;
    if (!kdl_parser::treeFromFile(urdf_file, tree)) {
        printf("Failed to construct kdl tree\n");
        return -1;

    KDL::Chain chain;
    if (!tree.getChain(base_link, tip_link, chain))
        printf("Couldn't find chain from %s to %s\n", base_link.c_str(), tip_link.c_str());

    KDL::ChainDynParam dyn(chain, KDL::Vector::Zero());

    KDL::JntArray q(chain.getNrOfJoints()); //dummy value
    KDL::JntSpaceInertiaMatrix H(chain.getNrOfJoints());
    dyn.JntToMass(q, H);

    printf("(0,0) element of Inertia Matrix %f\n", H(0,0));
    return 0;

The robot.urdf is kept along with test.cpp and headers are kept in a sub-directory. Below is the directory structure-

test@test:~/Desktop/calll/calling_so$ tree ...
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2016-11-15 07:41:47 -0500

Ravi Joshi gravatar image

I solved the problem. It is little strange at first. Below is the command, I tried and it worked-

g++ -L/opt/ros/indigo/lib -Iinclude -I/usr/include/eigen3 test.cpp -lorocos-kdl -lkdl_parser -o test

After compiling using above command, make sure the libraries are visible.

export LD_LIBRARY_PATH=/opt/ros/indigo/lib:$LD_LIBRARY_PATH
edit flag offensive delete link more

answered 2016-11-13 03:26:53 -0500

gvdhoorn gravatar image
error: ‘shared_ptr’ in namespace ‘std’ does not name a type
typedef std::shared_ptr<ModelInterface> ModelInterfaceSharedPtr;

This looks more like a C++11 vs Boost problem than anything else. Looks like urdf::ModelInterfaceSharedPtr is using std::shared_ptr (changed from boost::shared_ptr), but your compiler doesn't support it (or support hasn't been enabled).

Note btw that KDL is a stand-alone library: Only if you want to use the URDF parser(s) do you need to depend on kdl_parser (part of robot_model).

edit flag offensive delete link more


Hi gvdhoorn, thank you very much. There were some mismatch with the header files, which I have rectified just now. I updated the question. Can you please have a look again? Apologize for the inconvenience.

Ravi Joshi gravatar image Ravi Joshi  ( 2016-11-13 04:31:35 -0500 )edit

You'll have to figure out which libraries provide those symbols and link those in as well.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-14 04:38:51 -0500 )edit

One other thing: C++11 objects aren't necessarily binary compatible with non-C++11 objects / libraries. If is a C++03 binary, linking as you're doing might not work.

And btw: completely rewriting your question isn't really nice, I can't refer back to the errors you had before.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-14 04:40:30 -0500 )edit

Thanks a lot. I understand your concern. I tried compiling without C++11 and got the same errors. AFAIK all KDL:: are provided by orocos-kdl and kdl_parser:: are provided by kdl_parser. Also, I get exactly same errors after removing -L and -l compiler option. Weird. Isn't it?

Ravi Joshi gravatar image Ravi Joshi  ( 2016-11-14 05:03:46 -0500 )edit

I solved it. I posted my answer here . Thank you very much.

Ravi Joshi gravatar image Ravi Joshi  ( 2016-11-15 00:40:15 -0500 )edit

If you posted the solution to an external site, could you please answer your own question here, and include the relevant bits of whatever you posted externally? That keeps this site self-contained. If the external site ever goes down, we don't have an answer here otherwise.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-15 01:41:57 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-11-12 23:03:16 -0500

Seen: 327 times

Last updated: Nov 15 '16