在CPP中使用orocos-kdl和kdl_parser

时间:2016-11-13 15:48:16

标签: c++ g++

我在CPP中使用orocos-kdlkdl_parser。由于ROS安装在我的电脑上,我可以获取这些文件/opt/ros/indigo/lib/libkdl_parser.so/opt/ros/indigo/lib/liborocos-kdl.so.1.3.0。以下是主要代码,即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;
}

编译时,我看到许多未定义的引用错误。以下是终端的片段 -

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$

如何解决这些未定义的引用?

1 个答案:

答案 0 :(得分: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