0

File struct

Base|
      Foo |
          | Foo.hpp & Foo.cpp

      Bar |
          | Bar.hpp & Bar.cpp

      main.cpp & CMakeLists.txt

Bar.hpp

template <typename Ty>
class Bar {
public:
    float Value() const;
};    

Bar.cpp

#include "Bar.hpp"
template<typename Ty>
float Bar<Ty>::Value() const {
    return 1234.0;
}

Foo.hpp

#include "../Bar/Bar.hpp"
class Foo : public Bar<int> {
public:
    float Get() const;
};

Foo.cpp

#include "Foo.hpp"
float Foo::Get() const {
    return Value();
}

the main part of CMakeLists.txt

add_library(Bar_lib Bar/Bar.cpp Bar/Bar.hpp)
add_library(Foo_lib Foo/Foo.cpp Foo/Foo.hpp)
target_link_libraries(Foo_lib Bar_lib)
add_executable(testing main.cpp)
target_link_libraries(testing Foo_lib)

The main.cpp file just calls foo.Get(), and when I run cmake&make it gives

/usr/bin/ld: libFoo_lib.a(Foo.cpp.o): in function `Foo::Get() const':
Foo/Foo.cpp: undefined reference to `Bar<int>::Value() const'

But when I put everything into main.cpp file, it will successfully compile. So I know it has something to do with make. My question is how can I modify the CMakeLists.txt or even the cpp file to get this thing run? I'm a noob in cmake, so I'm sorry if I asked a dumb question. Big thanks to every suggestion

Emc Java
  • 21
  • 3

0 Answers0