【发布时间】: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 错误。由于我的库编译和链接成功,我不能认为这是一个编码错误。
【问题讨论】:
-
没有代码无法确定,但既然你提到“ball_popping.h/ball_popping.cpp”,听起来你遇到了Why can templates only be implemented in the header file?
-
那是正确的。我修改了代码以在头文件末尾包含一个 .tpp 文件的实现。感谢您的回复。
标签: c++ cmake static-libraries linker-errors undefined-reference