Understanding the concept of export symbols in shared libraries

3

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

I'm reading this 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 the line:

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

Here you are checking whether the FOX_DLL macro has been set. But at what point in the code is this macro set? I'm seeing the test #ifdef her but I'm not seeing her definition anywhere.

And the name? Is it free choice? Are there any limitations to choosing the name?

    
asked by anonymous 20.03.2016 / 20:02

1 answer

3

This is usually defined by the build. So it's usually defined by Make, another software building tool, or the script that calls the compiler or even the simple GCC command line.

There are other options but the description is an external symbol definition for the code.

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

    
20.03.2016 / 20:11