尽管链接了目标库,但未定义的引用错误

时间:2016-02-20 00:27:10

标签: c++ cmake static-libraries linker-errors undefined-reference

我在理解以下错误方面遇到了一些麻烦。

我在ball_popping.h / ball_popping.cpp中有一个类的声明/定义。这堂课是模板课。

我想将上面的内容编译成一个库并将它们链接到我的主文件game.cpp,该文件使用上述类的成员函数。

我的CMakeLists.txt如下,

CMakeFiles/ball_popping.dir/game.cpp.o: In function `int proficio_main<3ul>(int, char**, barrett::ProductManager&, barrett::systems::Wam<3ul>&, config)':
game.cpp:(.text._Z13proficio_mainILm3EEiiPPcRN7barrett14ProductManagerERNS2_7systems3WamIXT_EEE6config[int proficio_main<3ul>(int, char**, barrett::ProductManager&, barrett::systems::Wam<3ul>&, config)]+0x553): undefined reference to `ballpopping::BallPopping<3ul>::BallPopping(barrett::math::Matrix<3, 1, barrett::units::CartesianPosition> const&, UserGravComp<3ul>&, bool const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
game.cpp:(.text._Z13proficio_mainILm3EEiiPPcRN7barrett14ProductManagerERNS2_7systems3WamIXT_EEE6config[int proficio_main<3ul>(int, char**, barrett::ProductManager&, barrett::systems::Wam<3ul>&, config)]+0x1176): undefined reference to `ballpopping::BallPopping<3ul>::InEllipsoid(barrett::math::Matrix<3, 1, barrett::units::CartesianPosition> const&) const'
game.cpp:(.text._Z13proficio_mainILm3EEiiPPcRN7barrett14ProductManagerERNS2_7systems3WamIXT_EEE6config[int proficio_main<3ul>(int, char**, barrett::ProductManager&, barrett::systems::Wam<3ul>&, config)]+0x119a): undefined reference to `ballpopping::BallPopping<3ul>::IsDistanced(barrett::math::Matrix<3, 1, barrett::units::CartesianPosition> const&)'
CMakeFiles/ball_popping.dir/game.cpp.o:(.rodata._ZTVN11ballpopping11BallPoppingILm3EEE[vtable for ballpopping::BallPopping<3ul>]+0x28): undefined reference to `ballpopping::BallPopping<3ul>::operate()'
collect2: ld returned 1 exit status

图书馆成功编译和链接。可执行文件成功编译,但链接器抛出错误

{{1}}

BallPopping,InContact()和InEllipsoid()的构造函数在ball_popping.cpp中定义。

我想知道这是否是一个cmake错误。我不能认为它是编码错误,因为我的库成功编译和链接。

1 个答案:

答案 0 :(得分:1)

感谢@ user4581301的输入。在标题末尾的另一个文件中包含模板定义确实解决了这个问题。