Home » Linux » Explicitly exporting shared library functions in Linux

Explicitly exporting shared library functions in Linux

Posted by: admin November 30, 2017 Leave a comment

Questions:

Is there a Linux equivalent of __declspec(dllexport) notation for explicitly exporting a function from a shared library? For some reason with the toolchain I’m using, functions that aren’t class members don’t appear in the resulting shared library file.

Answers:
__attribute__((visibility("default")))

And there is no equivalent of __declspec(dllimport) to my knowledge.

#if defined(_MSC_VER)
    //  Microsoft 
    #define EXPORT __declspec(dllexport)
    #define IMPORT __declspec(dllimport)
#elif defined(__GNUC__)
    //  GCC
    #define EXPORT __attribute__((visibility("default")))
    #define IMPORT
#else
    //  do nothing and hope for the best?
    #define EXPORT
    #define IMPORT
    #pragma warning Unknown dynamic link import/export semantics.
#endif

Questions:
Answers:

http://gcc.gnu.org/wiki/Visibility

This is a complete tutorial on exporting in both msvc and gcc.