I am using orocos-kdl and kdl_parser in CPP. Since, ROS is installed in my pc, I can get these files /opt/ros/indigo/lib/libkdl_parser.so
and
/opt/ros/indigo/lib/liborocos-kdl.so.1.3.0
. Below is the main code i.e., 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;
}
While compiling, I see many undefined references errors. Below is the snippet from terminal-
test@test:~/Desktop/calll/calling_so$ g++ -I/opt/ros/indigo/include/ -I/usr/include/eigen3/ -L/opt/ros/indigo/lib/ -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
test@test:~/Desktop/calll/calling_so$
How to resolve these undefined references?
I solved the problem. It is little strange at first. Below is the command, I tried and it worked-
After compiling using above command, make sure the libraries are visible.