Note: Several related questions (e.g., this one) ended up being marked as duplicates of this question. I am aware of this particular question and follow the solution in the corresponding answers. However, different compilers yield different behavior and I don't know why.
My library features a class template and I would like to offer instances for certain template arguments in a library, as the template requires some significant compilation time. The class template could look like this (stack.hpp
)
#ifndef MY_STACK
#define MY_STACK
template<class T>
class stack
{
public:
stack();
};
#endif
and its implementation resides in a corresponding stack.tpp
file
#ifndef MY_STACK_TPP
#define MY_STACK_TPP
#include <iostream>
template<class T>
stack<T>::stack()
{
std::cout << "My stack constructor!" << std::endl;
}
#endif
As I would like to offer support for only certain template arguments, my stack.cpp
creates the following explicit template instances:
#include "stack.hpp"
template class stack<double>;
template class stack<char>;
#include "stack.tpp"
This compiles with g++ and clang++ but there are differences in the symbols of resulting shared library:
g++ -std=c++11 -c stack.cpp -o stack.so
nm -C stack.so | grep stack
0000000000000049 t _GLOBAL__sub_I_stack.cpp
0000000000000000 W stack<char>::stack()
0000000000000000 W stack<char>::stack()
0000000000000000 n stack<char>::stack()
0000000000000000 W stack<double>::stack()
0000000000000000 W stack<double>::stack()
0000000000000000 n stack<double>::stack()
vs.
clang++-7 -std=c++11 -c stack.cpp -o stack.so
nm -C stack.so | grep stack
0000000000000050 t _GLOBAL__sub_I_stack.cpp
In my application, the constructor of such an explicitly instantiated class is not found using clang++, but it works fine with g++. I figure this basic MWE gives the reason. Can anyone tell me how I can get the constructor symbols for my class template using clang++?