I am having following compilation issue with my project with template class
Undefined symbols for architecture x86_64:
"Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Node(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [apps/main] Error 1
make[1]: *** [apps/CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
my files are structured as follows:
├── include
│ └── maple
│ └── node.hpp
├── src
│ ├── CMakeLists.txt
│ └── node.cpp
├── apps
│ ├── CMakeLists.txt
│ └── main.cpp
└── CMakeLists.txt
/CmakeLists.txt
add_subdirectory(src)
add_subdirectory(apps)
/src/CMakeLists.txt
set(HEADER_LIST "${maple_SOURCE_DIR}/include/maple/node.hpp")
add_library(node_lib STATIC node.cpp ${HEADER_LIST})
target_include_directories(node_lib PUBLIC ../include)
/app/CMakeLists.txt
add_executable(main main.cpp)
target_link_libraries(main PRIVATE node_lib)
/include/maple/node.hpp
template <class T>
class Node
{
private:
int key;
T value;
public:
Node();
Node(int key, T value);
};
/src/node.cpp
template<class T>
Node<T>::Node() {
}
template<class T>
Node<T>::Node(int key, T value) {
key = key;
value = value;
}
template Node<std::string>;
/app/main.cpp
int main() {
Node<std::string> node(3, "abc");
return 0;
}
Can anyone guide me on how to compile this project right?
I am already aware that the template class needs the implementation along with the definition
I am asking how to achieve compilation using Cmake when .h and .cpp for template class are in different directories, as in a typical C++ project.