Boost logo

Boost :

Subject: Re: [boost] [config] GCC symbol visibility across shared libraries
From: Beman Dawes (bdawes_at_[hidden])
Date: 2010-05-20 15:01:22


On Thu, May 20, 2010 at 11:36 AM, Stewart, Robert
<Robert.Stewart_at_[hidden]> wrote:
> Beman Dawes wrote:
>> On Thu, May 20, 2010 at 10:21 AM, Stewart, Robert
>> <Robert.Stewart_at_[hidden]> wrote:
>> >
>> > Each dynamic library should define its own import/export
>> > macro to adorn the symbols of that library that should be
>> > exported/visible.  Those macros should be based upon
>> > BOOST_SYMBOL_EXPORT and BOOST_SYMBOL_IMPORT and whether the
>> > dynamic library is being built or used:
>> >
>> > #ifdef BUILDING_MYLIB
>> > #  define MYLIB_EXPORT BOOST_SYMBOL_EXPORT
>> > #else
>> > #  define MYLIB_EXPORT BOOST_SYMBOL_IMPORT
>> > #endif
>> >
>> > Using MYLIB_EXPORT for all public symbols in MyLib, and
>> > defining BUILDING_MYLIB only when compiling MyLib, will do
>> > what you want.
>>
>> See http://svn.boost.org/trac/boost/wiki/Guidelines/Separate for the
>> current guidelines, which most Boost libraries seem to be following.
>
> The #ifdef BOOST_HAS_DECLSPEC surrounds everything,

Yes, currently. But if BOOST_SYMBOL_EXPORT/IMPORT are always defined,
we can deprecate BOOST_HAS_DECLSPEC. It no longer will serve any
useful purpose except in legacy code that hasn't been updated to
reflect the macros always being present.

> but would be false for GCC, right?

BOOST_HAS_DECLSPEC wouldn't be defined for GCC <4 (or whatever release
it was they started supporting decoration, but then will be defined
for releases from then on.

> I'd suggest that the BOOST_HAS_DECLSPEC check be managed by the config system

If the three macros are always defined by the config system, there is
no long a need for the BOOST_HAS_DECLSPEC check

> so that BOOST_SYMBOL_EXPORT and BOOST_SYMBOL_IMPORT are defined appropriately (compressed into one place here):
>
> #define BOOST_SYMBOL_EXPORT
> #define BOOST_SYMBOL_IMPORT
> #define BOOST_SYMBOL_VISIBLE
>
> #ifdef BOOST_HAS_DECLSPEC
> #  undef BOOST_SYMBOL_EXPORT
> #  define BOOST_SYMBOL_EXPORT __declspec(dllexport)
> #  undef BOOST_SYMBOL_IMPORT
> #  define BOOST_SYMBOL_IMPORT __declspec(dllimport)
> #elif defined BOOST_HAS_VISIBILITY
> #  undef BOOST_SYMBOL_VISIBLE
> #  define BOOST_SYMBOL_VISIBLE __attribute__(visibility("default"))
> #  ifdef BOOST_DEFAULT_VISIBILITY_IS_HIDDEN
> #     undef BOOST_SYMBOL_EXPORT
> #     define BOOST_SYMBOL_EXPORT BOOST_SYMBOL_VISIBLE
> #  endif
> #endif
>
> BOOST_HAS_VISIBILITY would be undefined for most compilers, but would be defined for GCC (when __GNUC__ is defined, perhaps after some particular release, but I don't know when it was introduced).
>
> BOOST_DEFAULT_VISIBILITY_IS_HIDDEN must be defined for GCC, when __GNUC__ >= 4, but only if using -fvisiblity=hidden.  I haven't found a way to detect that, so I guess it has to be assumed or defined via the command line.
>
> Then, each library would use this instead:
>
> #define BOOST_WHATEVER_DECL
> #if defined BOOST_ALL_DYN_LINK || defined BOOST_WHATEVER_DYN_LINK
> #  undef BOOST_WHATEVER_DECL
> #  ifdef BOOST_WHATEVER_SOURCE
> #     define BOOST_WHATEVER_DECL BOOST_SYMBOL_EXPORT
> #  else
> #     define BOOST_WHATEVER_DECL BOOST_SYMBOL_IMPORT
> #  endif
> #endif

Here is the code I'm experimenting with for Boost.System:

// define BOOST_SYSTEM_DECL and BOOST_SYSTEM_VISIBLE
#if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_SYSTEM_DYN_LINK)
# if defined(BOOST_SYSTEM_SOURCE)
# define BOOST_SYSTEM_DECL BOOST_SYMBOL_EXPORT
# else
# define BOOST_SYSTEM_DECL BOOST_SYMBOL_IMPORT
# endif
# define BOOST_SYSTEM_VISIBLE BOOST_SYMBOL_VISIBLE
#else
# define BOOST_SYSTEM_DECL
# define BOOST_SYSTEM_VISIBLE
#endif

> Defining BOOST_WHATEVER_SOURCE in each library source file is rather awkward, but it does work.  (I prefer to define that in the build command line via makefile or whatever.  You can't forget it then.)

Defining it source files relieves a burden from users who choose to
setup their own builds. If you forget it during library development,
you are quickly reminded once you start testing.

>
>> >> 2)
>> >> What do you think about having BOOST_SYMBOL_EXPORT/IMPORT
>> >> always defined?
>> >
>> > If that's not what's being done -- I haven't looked at the
>> > patch itself -- then I agree.  Those macros should always be
>> > defined and each library must decide which to use as shown above.
>>
>> At the moment that isn't being proposed. I'm not against doing that,
>> and considered always defining all three of the new macros
>> (BOOST_SYMBOL_EXPORT/IMPORT/VISIBLE). That would imply changing all
>> compiler config headers, so I'd need help getting them right for
>> compilers I'm not familiar with.
>
> While the change I've shown is pervasive, the result is much simpler and cleaner for each library.

Yes, agreed. I've now got that on my do list.

--Beman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk