【问题标题】:Undefined reference error despite linking target libraries尽管链接了目标库,但未定义的引用错误
【发布时间】:2016-06-01 17:56:30
【问题描述】:

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

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

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

我的 CMakeLists.txt 如下,

set(EXECUTABLE_NAME ball_popping)
set(LIBRARY_NAME ball_popping_lib)

add_library(${LIBRARY_NAME} STATIC ball_popping.cpp ${INCLUDE_FILES})
add_executable(${EXECUTABLE_NAME} game.cpp)
target_link_libraries(${LIBRARY_NAME} ${Precompiled_LIBRARIES})          
target_link_libraries(${EXECUTABLE_NAME} ${LIBRARY_NAME})

库编译并链接成功。可执行文件编译成功,但链接器抛出错误

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

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

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

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 2022-09-27
    • 2020-08-10
    相关资源
    最近更新 更多