visibility attribute for classes?

visibility attribute for classes?

In g++ 3.4, the following marks the class and its members as "hidden" visibility.
icpc 9.1.043 ( for linux ) gives:
warning #1286: invalid attribute for "Foo"

class __attribute__((visibility("hidden"))) Foo
{
public:
Foo(){}
~Foo(){}
void dummy() {}
};

Placing the attribute before the "class" gets rid of the warninng but doesn't
make it "hidden". Placing it aftewards doesn't compile. Is there anyway
to do this with the intel c++ compiler without marking every method?
Thanks.

6 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione

For some reason, ICC has dislikes GCC-3.4's visibility attribute. ICC detects that you are using GCC-3.4 and decides to be "compatible" by not accepting the attribute. If you want this to work, you need to tell ICC to be compatible with a newer GCC, e.g., GCC-4.0.

So although your present usage does not work:

$ icpc -gcc-version=340 -c -o attr.o t1.cc
t1.cc(2): warning #1286: invalid attribute for "Foo"
__attribute__((visibility("hidden")))
^

This should work:

$ icpc -gcc-version=400 -c -o attr.o t1.cc
<>

Have a look at the -fvisibility=hidden option for broader applicability.

Ritratto di John O (Intel)

I investigated this & icc duplicates the warningsof differentgcc versions. If you are using gcc 4.0 & higher, icc will not issue a warning, but if you are using ealier versions of gcc both compilers issue a warning.

I'm having problems seeing the what difference __attribute__((visibility("hidden"))) Foo makes. If I add this to your example:

#ifdef WORKAROUND
class Foo
#else
class __attribute__((visibility("hidden"))) Foo
#endif

and compile with gcc 4.1.1 & don't see a difference:

$ g++ -c class-attribute-hidden2.cpp; nm -C class-attribute-hidden2.o
0000000000000000 T useFoo()
0000000000000000 W Foo::dummy()
0000000000000000 W Foo::Foo()
0000000000000000 W Foo::~Foo()
U __gxx_personality_v0

$ g++ -DWORKAROUND -c class-attribute-hidden2.cpp; nm -C class-attribute-hidden2.o
0000000000000000 T useFoo()
0000000000000000 W Foo::dummy()
0000000000000000 W Foo::Foo()
0000000000000000 W Foo::~Foo()
U __gxx_personality_v0

There is an intersting discussion of Visibility here http://gcc.gnu.org/wiki/Visibilitybut I haven't figured this out what marking the class as hidden does or if icc has different behavior than gcc. Thanks for the feedback,

_|ohnO

The hidden attribute is probably primarily used for shared libraries. Keeping
unnecassary symbols hidden can give a performance boost on library loading
because the loader doesn't have to do so many symbol relocations.
So from your example:
$g++ -shared -fpic vis.cpp -o libvis.so && nm -C libvis.so
....
000004d0 T useFoo()
0000050c t Foo::dummy()
00000512 t Foo::Foo()
00000506 t Foo::~Foo()
....

$g++ -DWORKAROUND -shared -fpic vis.cpp -o libvis.so && nm -C libvis.so
....
00000584 T useFoo()
000005d0 W Foo::dummy()
000005d6 W Foo::Foo()
000005ca W Foo::~Foo()
....

I have a similar problem. I compile with ICC 13.2 on a machine with GCC 4.3.2 and I get warning #1286: invalid attribute for "Foo". If I add -gcc-version=400 I don't get the warning. Up to GCC version 4.2.1 it is fine but with newer version of GCC the problem persists.

I want to have my compilation compatible with GCC 4.3.2 but at the same time I want to eliminate the problem. Any idea of how I can work arround it?

>>...I want to have my compilation compatible with GCC 4.3.2 but at the same time I want to eliminate the problem. Any idea of how
>>I can work arround it?

#ifdef-#else-#endif solution ( suggested by John O ) is a classic one and it is used for years for similar cases.

Accedere per lasciare un commento.