尽管链接了目标库,但仍出现未定义的引用错误
Undefined reference error despite linking target libraries
我在理解以下错误时遇到了一些麻烦。
我在 ball_popping.h/ball_popping.cpp 中有 declaration/definition 个 class。 class 是模板化的 class.
我想将上面的编译为一个库,并 link 它们针对我的主文件 game.cpp,它使用上面 class.
的成员函数
我的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})
库编译成功,links。可执行文件编译成功但 linker 抛出错误
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 错误。我不认为这是一个编码错误,因为我的库编译成功 links。
感谢@user4581301 的输入。在 header 末尾包含来自另一个文件的模板定义确实解决了问题。
我在理解以下错误时遇到了一些麻烦。
我在 ball_popping.h/ball_popping.cpp 中有 declaration/definition 个 class。 class 是模板化的 class.
我想将上面的编译为一个库,并 link 它们针对我的主文件 game.cpp,它使用上面 class.
的成员函数我的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})
库编译成功,links。可执行文件编译成功但 linker 抛出错误
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 错误。我不认为这是一个编码错误,因为我的库编译成功 links。
感谢@user4581301 的输入。在 header 末尾包含来自另一个文件的模板定义确实解决了问题。