1

I was just reading "Creating Library with backward compatible ABI that uses Boost". Now I have a class that is supposed to be serialised using boost. How can I hide that part from the public interface? In the article mentioned it was suggested to use the -fvisibility=hidden option of gcc. I tried to implement this in a test class:

#define LOCAL  __attribute__ ((visibility ("hidden")))
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/shared_ptr.hpp>

class hideBoost
{
public:
    int visible;
    LOCAL boost::shared_ptr<int> notVisible;

private:
    friend class boost::serialization::access;
    template<class archive>
    void serialize(archive& ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(visible);
        ar & BOOST_SERIALIZATION_NVP(notVisible);
    }
};

And my main program is:

#include <iostream>
#include "HideBoost.hpp"
int main()
{
    std::cout<<"start"<<std::endl;
    hideBoost b;
    b.visible=5;
    b.notVisible=10;
    std::cout<<"end"<<std::endl;
}

Both are compiled using gcc-4.6 on MacOSX with the flag -fvisibility=hidden. EDIT: The main program does not have to boost headers at compile time and therefore compilation fails. I do not want to pass the boost headers to the main program since this can lead to incompatibilities on systems that have more than one version of boost installed (I guess this is called ABI incompatibility).

Now my question:

1) Is the member variable notVisible in my example properly hidden from the public interface?

2) EDIT: How can I hide the serialisation routine and the header files?

I'm thankful to any hints how this can be done properly. A solution that works using gcc and clang on unix and OS X would be great. Thanks in advance!

Best wishes, Peter

Community
  • 1
  • 1
hansgans
  • 307
  • 4
  • 12
  • As HIF said, visibility is not the right tool. But what are you _really_ trying to achieve? "How can I hide that part from the public interface?": what interface? The header file? – YSC Dec 05 '16 at 08:27
  • I updated my original post. The main goal is to avoid problems on systems that have multiple versions of boost installed. – hansgans Dec 05 '16 at 08:34

1 Answers1

1

__attribute__ ((visibility ("hidden"))) has nothing to do with your problem. You should declare notVisible as a private member of that class.

__attribute__ ((visibility ("hidden"))) just tells the dynamic linker that you can't link a symbol to the function/variable being guarded by this attribute.

Note that you can't hide a symbol from nm or objdump -T list, no matter it's "hidden" or "default" . "default" means that the function/variable which this attribute is being applied to is supposed to be exported from our library whereas "hidden" isn't.

frogatto
  • 28,539
  • 11
  • 83
  • 129