c++visibility

hidden visibility for a c++ header only library


i am trying to understand C++ visibility in respect to a header only library and want to make sure that i have the right understanding regarding it. I am currently using Clang 15.0.6 compiler.

So from what i understand/read if i have methods in a shared library A.so as shown below

#define ATTRIBUTE_HIDDEN __attribute__((__visibility__("hidden")))

ATTRIBUTE_HIDDEN int foo (){
   return 5;
}
int bar(){
   return 6;
}

Than if we build A.so with -fvisibility=hidden flag it will force the visibility of all symbols marked with ATTRIBUTE_HIDDEN macro to be hidden, which prevents them from being visible across library boundaries. Therefore if B.so now links with A.so than code inside B.so can only invoke bar() method and not foo() method. Is that correct.

Moreover what if now we have a header only library C add_library(C INTERFACE .)and let says it contains a util.h which has the following methods

#define ATTRIBUTE_HIDDEN __attribute__((__visibility__("hidden")))

inline ATTRIBUTE_HIDDEN int xxx (){
   return 5;
}
inline int yyy(){
   return 6;
}

To me seems like if now some another shared library D.so links to it than D.so will be able to invoke both xxx() and yyy() method from the header only library C but if we build D.so with -fvisibility=hidden flag than if another shared library E.so links to D.so than inside E.so we can only invoke yyy() and not xxx() due to the later function marked hidden. is my understanding correct or i am missing something here.


Solution

  • Your basic understanding of visibility for .so files is correct. That's why I'm a bit surprised about your confusion. Header-only libraries by definition do not have .so files, which is why visibility does not matter at all. Header-only libraries are consumed by the compiler. Any .cpp file including the header-only library can call those library functions.