Calling ROS libraries from external CPP
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-
- /opt/ros/indigo/lib/libkdl_parser.so
- /opt/ros/indigo/lib/liborocos-kdl.so.1.3.0
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
test@test:~/Desktop/calll/calling_so$
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 ...