I'm learning to use __attribute__ ((visibility("default")))
for symbol export
// a.cpp
class A
{
public:
__attribute__ ((visibility("default"))) void func() {;};
__attribute__ ((visibility("default"))) int cnt;
};
But I ran into the following problem
# g++ -c a.cpp
a.cpp:5:50: warning: ‘visibility’ attribute ignored [-Wattributes]
__attribute__ ((visibility("default"))) int cnt;
^~~
What's the difference between a member function and a member variable? Why can one export a symbol and the other cannot?
Member functions are really just ordinary functions with special signature to accomodate the hidden this
argument. So you can attach visibility attributes to them like to other global functions.
On the contrary, member variables do not correspond to global entities - they are just symbolic names for offsets inside memory occupied by object of the class. So there is no point in attaching visibility to them.