C++ templates and visibility("hidden")

Stefan Strasser sstrasser@systemhaus-gruppe.de
Sat Apr 9 15:23:00 GMT 2005

> Okay, I did s,MY_API class,class MY_API. Now, when using colorgcc, I even saw 
> the warning you hinted me :)
> However, when playing a bit around, I see, that I've to explicitely do:
> template<class T>
> void FOO_API TFoo<T>::bar(const T& var) {
>     std::cout << "TFoo::bar(" << var << ")" << std::endl;
> }

no you don't.
I don't know what's wrong with your example but the following works:

template<typename T>
struct API A{
   void f();

test.cpp: -fvisibility=hidden -shared
#define API __attribute__((visibility("default")))
#include "test.h"

template<typename T>
void A<T>::f(){}

template class A<int>;

test2.cpp: -ltest
#define API
#include "test.h"

int main(){
   A<int> a;

Stefan Strasser

More information about the Gcc-help mailing list