1

I am having some trouble in understanding the following error.

I have declaration/definition of a class in ball_popping.h/ball_popping.cpp. The class is a templated class.

I want to compile the above as a library and link them against my main file, game.cpp which uses the member functions of the above class.

My CMakeLists.txt is as below,

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})

The library compiles and links successfully. The executable compiles successfully but the linker throws an error

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

The constructor for BallPopping, InContact() and InEllipsoid() are defined within ball_popping.cpp.

I would like to know if this is a cmake error. I cannot think it of being a coding error since my library compiles and links successfully.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
ZincFur
  • 49
  • 9
  • 3
    Can't be certain without code, but since you mention "ball_popping.h/ball_popping.cpp" it sounds like you've run into [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user4581301 Feb 20 '16 at 01:00
  • That was spot on. I modified the code to include the implementation as a .tpp file at the end of the header file. Thank you for your response. – ZincFur Feb 20 '16 at 19:06

1 Answers1

1

Thanks to @user4581301 for his input. Including the template definitions from another file at the end of the header did solve the problem.

ZincFur
  • 49
  • 9