使用 gtest (C++) 时链接器无法找到对象引用
Linker unable to find object references when using gtest (C++)
我决定开始单元测试,并使用 gtest 编写了我的第一个单元测试。这是来源:
DetectorTests.cpp:
#include <gtest/gtest.h>
#include <ros_layer/utils/DetectorUtils.h>
class DetectorTests : public ::testing::Test {
public:
DetectorTests() {
}
bool initTestFixture() {
return true;
}
virtual void SetUp() {
ASSERT_TRUE(initTestFixture());
}
virtual void TearDown() {
}
};
// Test where the robot does a vector operation
TEST_F(DetectorTests, testNoDetect) {
// Parameters get set up here
ros::NodeHandle privateNodeHandle("~");
std::shared_ptr<ros_layer::DetectorUtils> distDetector = std::make_shared<ros_layer:DetectorUtils>(privateNodeHandle);
// @to-do set up object parameters before running test
ASSERT_FALSE(distDetector->checkReadings());
}
int main(int argc, char** argv) {
ros::init(argc, argv, "detection_action_test");
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我的 CMakeLists.txt 有以下几行:
find_package(GTest REQUIRED)
catkin_add_gtest(detector-test test/DetectorTests.cpp)
target_link_libraries(detector-test ${catkin_LIBRARIES})
当我 运行 CMake 使用 运行 的参数进行测试时,我在链接时收到此错误:
In function `construct<ros_layer::DetectorUtils, ros::NodeHandle&>':
/code/include/c++/4.8/ext/new_allocator.h:120: undefined reference to `ros_layer::DetectorUtils::DetectorUtils(ros::NodeHandle&)'
CMakeFiles/detector-test.dir/test/DetectorTest.cpp.o: In function `destroy<ros_layer::DetectorUtils>':
/code/include/c++/4.8/ext/new_allocator.h:124: undefined reference to `ros_layer::DetectorUtils::~DetectorUtils()'
如果我删除所有与单元测试相关的代码并只编写带有正在创建的 DetectorUtils 对象的 c++,我一点问题都没有。是什么导致了这个错误,我该如何解决?
解决了。 target_link_libraries 在我的 CMakeLists 中需要有定义 DetectorUtils 的库。这是更新的行:
target_link_libraries(dragging-test ROSLayerLibrary ${catkin_LIBRARIES})
我决定开始单元测试,并使用 gtest 编写了我的第一个单元测试。这是来源:
DetectorTests.cpp:
#include <gtest/gtest.h>
#include <ros_layer/utils/DetectorUtils.h>
class DetectorTests : public ::testing::Test {
public:
DetectorTests() {
}
bool initTestFixture() {
return true;
}
virtual void SetUp() {
ASSERT_TRUE(initTestFixture());
}
virtual void TearDown() {
}
};
// Test where the robot does a vector operation
TEST_F(DetectorTests, testNoDetect) {
// Parameters get set up here
ros::NodeHandle privateNodeHandle("~");
std::shared_ptr<ros_layer::DetectorUtils> distDetector = std::make_shared<ros_layer:DetectorUtils>(privateNodeHandle);
// @to-do set up object parameters before running test
ASSERT_FALSE(distDetector->checkReadings());
}
int main(int argc, char** argv) {
ros::init(argc, argv, "detection_action_test");
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我的 CMakeLists.txt 有以下几行:
find_package(GTest REQUIRED)
catkin_add_gtest(detector-test test/DetectorTests.cpp)
target_link_libraries(detector-test ${catkin_LIBRARIES})
当我 运行 CMake 使用 运行 的参数进行测试时,我在链接时收到此错误:
In function `construct<ros_layer::DetectorUtils, ros::NodeHandle&>':
/code/include/c++/4.8/ext/new_allocator.h:120: undefined reference to `ros_layer::DetectorUtils::DetectorUtils(ros::NodeHandle&)'
CMakeFiles/detector-test.dir/test/DetectorTest.cpp.o: In function `destroy<ros_layer::DetectorUtils>':
/code/include/c++/4.8/ext/new_allocator.h:124: undefined reference to `ros_layer::DetectorUtils::~DetectorUtils()'
如果我删除所有与单元测试相关的代码并只编写带有正在创建的 DetectorUtils 对象的 c++,我一点问题都没有。是什么导致了这个错误,我该如何解决?
解决了。 target_link_libraries 在我的 CMakeLists 中需要有定义 DetectorUtils 的库。这是更新的行:
target_link_libraries(dragging-test ROSLayerLibrary ${catkin_LIBRARIES})