0
我在理解以下錯誤時遇到了一些麻煩。儘管鏈接目標庫,但未定義的引用錯誤
我有一個類的聲明/定義在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」,這聽起來像是你已經遇到了[爲什麼模板只能在頭文件中實現?](http:// stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user4581301
這是現貨。我修改了代碼,將實現作爲一個.tpp文件包含在頭文件的末尾。感謝您的答覆。 – ZincFur