Search code examples
c++eigendrake

How can I fix the "undefined reference to" error in cpp, when call for functions in rigidbodytree, like doKinematics, CreateKinematicCache


I am a starter of drake. I use urdf parser to extract my robot model to RigidBodyTree. Then I use doKinematics function to create kinematic cache. However, the builder give me the error below.
This is one case, in some cases, I cannot use other functions in the file rigid_body_tree.cc, and errors are almost the same one.

  • OS:Ubuntu 16.04
  • Language: C++ -GCC 5.4.0 -Clang 6.0.0 -Python 2.7.12
  • Built from source
    • Bazel 0.28.1
    • Bazel C++ compiler +capture_cc_env=external/drake/tools/cc_toolchain/capture_cc.env +source external/drake/tools/cc_toolchain/capture_cc.env ++BAZEL_CC=/usr/bin/gcc ++BAZEL_CC_FLAGS= +/usr/bin/gcc --version gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
    • Git revision: 800d961
    • Build drake using the method on the first page of drake document, "installation and quickstart
auto tree = new RigidBodyTree<double>();  
Eigen::Matrix<double, 9, 1> q_in, qd_in;  
q_in << 1, 2, 3, 4, 5, 6, 7, 8, 9;  
qd_in << 1, 2, 3, 4, 5, 6, 7, 8, 9;  
auto kinematics = tree->doKinematics(q_in, qd_in);

Error info:
test.pic.o:test.cpp:function main: error: undefined reference to 'KinematicsCache<Eigen::Matrix<double, 9, 1, 0, 9, 1>::Scalar> RigidBodyTree<double>::doKinematics<Eigen::Matrix<double, 9, 1, 0, 9, 1>, Eigen::Matrix<double, 9, 1, 0, 9, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, 9, 1, 0, 9, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 9, 1, 0, 9, 1> > const&, bool) const'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Solution

  • You can change

    Eigen::Matrix<double, 9, 1> q_in, qd_in; 
    

    to

    Eigen::VectorXd q_in(9);
    Eigen::VectorXd qd_in(9); 
    

    The problem is that we explicitly instantiate the template for doKinematics in https://github.com/RobotLocomotion/drake/blob/004864a1ef09583754b8573f287fd2658d18aab3/attic/multibody/rigid_body_tree.cc#L3878-L3884, and it doesn't include the template with type Eigen::Matrix<double, 9, 1>. Hence the linker cannot find a reference. But we do instantiate the template with type Eigen::VectorXd.

    BTW, RigidBodyPlant is deprecated. We recommend the user to try MultibodyPlant instead.