Understanding the concept of export Symbols in shared libraries

Asked

Viewed 48 times

3

I’m reading about this concept of exporting elements out of a shared library (DLL or OS).

I’m reading that one existing publication on the GCC Wiki.

The last example of the publication is this:

// Generic helper definitions for shared library support
#if defined _WIN32 || defined __CYGWIN__
  #define FOX_HELPER_DLL_IMPORT __declspec(dllimport)
  #define FOX_HELPER_DLL_EXPORT __declspec(dllexport)
  #define FOX_HELPER_DLL_LOCAL
#else
  #if __GNUC__ >= 4
    #define FOX_HELPER_DLL_IMPORT __attribute__ ((visibility ("default")))
    #define FOX_HELPER_DLL_EXPORT __attribute__ ((visibility ("default")))
    #define FOX_HELPER_DLL_LOCAL  __attribute__ ((visibility ("hidden")))
  #else
    #define FOX_HELPER_DLL_IMPORT
    #define FOX_HELPER_DLL_EXPORT
    #define FOX_HELPER_DLL_LOCAL
  #endif
#endif

// Now we use the generic helper definitions above to define FOX_API and FOX_LOCAL.
// FOX_API is used for the public API symbols. It either DLL imports or DLL exports (or does nothing for static build)
// FOX_LOCAL is used for non-api symbols.

#ifdef FOX_DLL // defined if FOX is compiled as a DLL
  #ifdef FOX_DLL_EXPORTS // defined if we are building the FOX DLL (instead of using it)
    #define FOX_API FOX_HELPER_DLL_EXPORT
  #else
    #define FOX_API FOX_HELPER_DLL_IMPORT
  #endif // FOX_DLL_EXPORTS
  #define FOX_LOCAL FOX_HELPER_DLL_LOCAL
#else // FOX_DLL is not defined: this means FOX is a static lib.
  #define FX_API
  #define FOX_LOCAL
#endif // FOX_DLL

My doubt is at the beginning of the second part, more precisely in line:

#ifdef FOX_DLL // defined if FOX is compiled as a DLL

Here is being done a check if the macro FOX_DLL was set. But at what point in the code is this macro set? I’m seeing the test #ifdef but I’m not seeing her definition anywhere.

And the name? Is it free choice? There are limitations to the choice of name?

1 answer

3


Usually this is defined by the compilation. So normally it is defined by Make, another software build tool or by script which calls the compiler or the simple GCC command line.

There are other options but the description is a symbol definition external to the code.

To compiler option that allows you to do this is the -DFOX_DLL.

It’s any symbol, following the rules of any symbol, making sure it doesn’t conflict with anything, you can create whatever you want, preferably something that makes sense.

  • What about the name? It’s free choice?

  • Yes, it is a symbol, following the rules of any symbol, being sure that it will not conflict with anything, can create what you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.