login
Header Space

 
 

g++: using visibility attribute with explicit template instances

October 16, 2006 - 12:38pm
Submitted by Anonymous on October 16, 2006 - 12:38pm.
Applications and Utilities

I hope this thread is appropriate for this forum.

I recently began using the visibility attribute now available in gcc.

The software I am working on is proprietary (my employer pays the bills ;) ). We have several modules, each built as a separate DSO library. One of these has a large template class with several specialized methods. We do not provide the implementations on the header file, so this means we limit our users to only those template instances that we explicitly instantiate in the implementation file.

I cannot seem to get this template class working with g++ and its visibility attribute. Below is much simplified example that demonstrates my problem. Environment is Ubuntu Dapper, g++ version 4.0.3, kernel version 2.6.15-27-686.

Error message:
----------------------

/tmp/ccsVuuf1.o: In function `main':TestMain.cpp:(.text+0x33): undefined reference to `TestClass::test(double const&) const'
collect2: ld returned 1 exit status

Build commands:
------------------------

g++ -c -O3 -fpic -fvisibility=hidden -fvisibility-inlines-hidden -std=c++98 -pedantic TestClass.cpp
g++ -shared -o libTestClass.so TestClass.o
g++ -o TestMain TestMain.cpp -L. -lTestClass

TestClass.hpp:
---------------------

#ifndef HPP_TESTCLASS
#define HPP_TESTCLASS
template 
class __attribute__ ((visibility("default"))) TestClass
{
public:
    bool test(_Tp const& x) const;
};
#endif

TestClass.cpp:
---------------------

#include "TestClass.hpp"
#include 
template 
bool
TestClass<_Tp>::
test(_Tp const& x) const
{ return typeid(x) == typeid(_Tp); }
template class __attribute__ ((visibility("default"))) TestClass;

TestMain.cpp:
-------------------

#include "TestClass.hpp"
int main()
{
    TestClass t;
    t.test();
    return 0;
}

Thanks in advance for any insight that you can provide.

-Matthew-

templates and visibility mix

October 16, 2006 - 6:11pm
Anonymous (not verified)

templates and visibility mix since g++ 4.1 only. you should upgrade. this is a bug in gcc < 4.1. at least if i understood your problem

I have gcc 4.1 on a non-work

October 16, 2006 - 9:50pm
Anonymous (not verified)

I have gcc 4.1 on a non-work platform. I will try it out. Thanks for the advice; if that's it then at least I might have a workaround.

-Matthew-

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
speck-geostationary