2016-11-13 35 views
0

我在CPP中使用orocos-kdlkdl_parser。由於ROS安裝在我的電腦中,我可以得到這些文件/opt/ros/indigo/lib/libkdl_parser.so/opt/ros/indigo/lib/liborocos-kdl.so.1.3.0。下面是主碼,即test.cpp -在CPP中使用orocos-kdl和kdl_parser

#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; 
} 

在編譯時,我看到許多未定義的引用錯誤。以下是來自終端的片段 -

[email protected]:~/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 
[email protected]:~/Desktop/calll/calling_so$ 

如何解決這些未定義的引用?

回答

0

我解決了這個問題。起初有點奇怪。下面是命令,我試過,它的工作 -

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

編譯後使用上述命令,確保庫是可見的。

export LD_LIBRARY_PATH=/opt/ros/indigo/lib:$LD_LIBRARY_PATH