当catkin_make run_tests时,Gtest失败。未定义参考

时间:2019-07-10 17:15:00

标签: c++ googletest

我的catkin_make以某种方式成功执行,但run_test失败。我已经确认我已将测试脚本和代码作为可执行文件添加到CMakeList.txt中,并使用target_link_libraries进行了链接。但是,在运行Catkin_make run_tests时,我总是收到错误消息:

undefined reference to `VisualRobot::VisualRobot(std::shared_ptr<moveit::core::RobotModel>)'

这是我的测试代码:

#include <iostream>
#include <moveit_visual_tools/moveit_visual_tools.h>
#include <visualization_msgs/Marker.h>
#include <eigen_conversions/eigen_msg.h>
#include <tf/transform_datatypes.h>
#include <cmath>
#include "gtest/gtest.h"
#include "visual_robot.h"

class Checksweep : public testing::Test{
public: 

  virtual void SetUp() {
    // ...
    robot_model_loader::RobotModelLoader robot_model_loader("robot_description");
    robot_model::RobotModelPtr kinematic_model = robot_model_loader.getModel();
    visual = new VisualRobot(kinematic_model);
  }

  virtual void TearDown() { delete visual; }

  VisualRobot * visual;
  ros::NodeHandle nh;

};

TEST_F(Checksweep, TestnJoint)
{
  EXPECT_GT(visual->nJoints, 0); //nJoints is a public variable in VisualRobot class.
}

int main(int argc, char **argv){
  testing::InitGoogleTest(&argc, argv);
  ros::init(argc, argv, "my_testnode");
  return RUN_ALL_TESTS();
}

还有我的visual_robot.h:

#ifndef _VISUAL_ROBOT
#define _VISUAL_ROBOT

class VisualRobot{
public:
    VisualRobot( robot_model::RobotModelPtr kinematic_model);
    void make_function();


    robot_model::RobotModelPtr kinematic_model;
    int nJoints;

private:
    ros::NodeHandle nh;
    ros::Publisher marker_pub;

};

#endif

根据我的理解,我认为我已经在测试代码的VisualRobot * visual行中定义了Visual Robot的实例

编辑1:同样,该错误似乎发生在我的测试代码行中:

visual = new VisualRobot(kinematic_model);

编辑2:添加了我的CMakeList.txt的一部分:

add_executable(visual_robot src/visual_robot.cpp)
target_link_libraries(visual_robot ${catkin_LIBRARIES} ${Boost_LIBRARIES})

if(CATKIN_ENABLE_TESTING)

  find_package(rostest REQUIRED)
  add_rostest_gtest(visual_testnode launch/visual.launch src/visual_test.cpp )
  target_link_libraries(visual_testnode ${catkin_LIBRARIES} )
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
endif()

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。答案的来源在这里: enter link description here

基本上,在我的CMakerList.txt中,我需要add_library(visual_robot src/visual_robot.cpp) 并使用target_link_libraries(visual_testnode visual_robot ${catkin_LIBRARIES})

将其链接到我的测试文件
相关问题