none
Compile dynamic library without __declspec(dllexport)

    Question

  • Is it possible to compile a dynamic library, without having __declspec(dllexport) before each functio you want to be available? I want all of the functions I write to be available. Since I also write code for linux, i am not used to put it in front of every function declaration. Is there an option to make, as i said, __declspec(dllexport) default for every function the compiler encounters?

    Grtz, The Doctor
    Wednesday, March 04, 2009 12:20 PM

Answers

  • I don't know of a way to make VC++ automatically export all symbols.  However, you can put some kind of export macro in front of all symbols in your library and make sure that when compiling under other compilers, it is silently removed by doing something like this:

    #ifdef _MSC_VER
    #ifdef MYLIB_EXPORTS
    #define MYLIB_API __declspec(dllexport)
    #else
    #define MYLIB_API __declspec(dllimport)
    #endif
    #else
    #define MYLIB_API
    #endif

    • Marked as answer by Nancy Shao Monday, March 09, 2009 11:36 AM
    Wednesday, March 04, 2009 1:56 PM

All replies

  • Using a .def file is another way, but that's not what you are looking for.  Hard to believe Linux does this, perhaps you are confusing static libraries (big on *nix) with DLLs.
    Hans Passant.
    Wednesday, March 04, 2009 12:36 PM
    Moderator
  • I don't know of a way to make VC++ automatically export all symbols.  However, you can put some kind of export macro in front of all symbols in your library and make sure that when compiling under other compilers, it is silently removed by doing something like this:

    #ifdef _MSC_VER
    #ifdef MYLIB_EXPORTS
    #define MYLIB_API __declspec(dllexport)
    #else
    #define MYLIB_API __declspec(dllimport)
    #endif
    #else
    #define MYLIB_API
    #endif

    • Marked as answer by Nancy Shao Monday, March 09, 2009 11:36 AM
    Wednesday, March 04, 2009 1:56 PM
  • Ok, I think Nick's way is the best solution.

    Note to Hans:

    No I am not, I am compiling the same code with linux, and it works without some export note to the compiler.

    Thanks people!
    Wednesday, March 04, 2009 2:33 PM
  • Hi The Doctor,

    I was looking for this link earlier, but couldn't find it until now.  It provides a similar approach that lets you control exported symbols with gcc (at least in recent versions) the same way MSVC does it.  It might be a good approach for cross-platform code too:

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


    Wednesday, March 04, 2009 2:37 PM
  • Hmya, 5 minutes to load a DLL, 6 megabyte export tables.  "It works" is a stretch.
    Hans Passant.
    Wednesday, March 04, 2009 2:47 PM
    Moderator
  • In addition to what Nick says, if you then put all your functions in a class and make them all static, they will all be exported if you export the class itself.

    class MYLIB_API CMyUtilityFunctions
    {
    public:
        static void MyFunc1();
        static void MyFunc2();
    etc
    };
    Friday, March 06, 2009 1:00 AM