Uploaded image for project: 'Lucy'
  1. Lucy
  2. LUCY-231

Symbol visibility

    XMLWordPrintableJSON

Details

    • Task
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • None
    • Clownfish
    • None

    Description

      In order to get compiled extension working on Windows, we'll have to define the visibility of our extern variables and functions. On Windows every exported symbol of a DLL has to be marked with __declspec(dllexport) when compiling the DLL. If you're linking against a DLL, every symbol imported from the DLL has to be marked __declspec(dllimport)

      On UNIX, every symbol is exported by default, so defining visibility is not strictly necessary. But hiding symbols that don't have to be exported has the benefit of reducing size and speeding up loading of a DLL. Hidden symbols also allow the compiler to generate more optimized code.

      The standard approach is to compile with the GCC option -fvisibility=hidden which emulates the Windows behavior. Then a macro is defined roughly like that (should probably be handled by charmonizer):

      #if defined __GNUC__
      #  if defined _WIN32 || defined __CYGWIN__
      #    define CHY_EXPORT __attribute__ ((dllexport))
      #  elif __GNUC__ >= 4
      #    define CHY_EXPORT __attribute__ ((visibility ("default")))
      #  else
      #    define CHY_EXPORT
      #  endif
      #elif defined _MFC_VER
      #  define CHY_EXPORT __declspec(dllexport)
      #else
      #  define CHY_EXPORT
      #endif
      

      This macro can then be used like that:

      CHY_EXPORT void
      exported_function();
      
      extern CHY_EXPORT int exported_variable;
      

      When compiling an extension, we also have to handle __declspec(dllimport) on Windows. For the generated headers, we could define CHY_IMPORT and use it instead of CHY_EXPORT for "included" headers. For the code in XSBind.[ch], we could define BUILDING_XSBIND only during compilation of XSBind.c and then use something like that:

      #if defined __GNUC__
      #  if defined _WIN32 || defined __CYGWIN__
      #    if BUILDING_XSBIND
      #      define XSBIND_EXPORT __attribute__ ((dllexport))
      #    else
      #      define XSBIND_EXPORT __attribute__ ((dllimport))
      #    endif
      #  elif __GNUC__ >= 4
      #    define XSBIND_EXPORT __attribute__ ((visibility ("default")))
      #  else
      #    define XSBIND_EXPORT
      #  endif
      #elif defined _MFC_VER
      #  if BUILDING_XSBIND
      #    define XSBIND_EXPORT __declspec(dllexport)
      #  else
      #    define XSBIND_EXPORT __declspec(dllimport)
      #  endif
      #else
      #  define XSBIND_EXPORT
      #endif
      
      XSBIND_EXPORT cfish_Obj*
      cfish_XSBind_new_blank_obj(SV *either_sv);
      

      Attachments

        1. bad_encapsulation.pl
          4 kB
          Marvin Humphrey
        2. bad_encapsulation_report.txt
          3 kB
          Marvin Humphrey
        3. 0006-Install-import-library-on-Windows.patch
          4 kB
          Nikolas Wellnhofer
        4. 0005-Switch-to-fvisibility-hidden-and-start-using-CHY_EXP.patch
          12 kB
          Nikolas Wellnhofer
        5. 0004-Preliminary-Charmonizer-support-for-symbol-export.patch
          16 kB
          Nikolas Wellnhofer
        6. 0003-Unify-method-data-for-initialization-and-callbacks.patch
          25 kB
          Nikolas Wellnhofer
        7. 0002-Switch-to-fvisibility-hidden-and-start-using-CHY_EXP.patch
          8 kB
          Nikolas Wellnhofer
        8. 0001-Preliminary-Charmonizer-support-for-symbol-export.patch
          10 kB
          Nikolas Wellnhofer

        Activity

          People

            nwellnhof Nikolas Wellnhofer
            nwellnhof Nikolas Wellnhofer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: