开发者

Exporting symbols when compiling DLL - MSVC

I have a shared library which uses CMake as build system. It compiles fine on Linux machines with GCC. Now I am trying to compile on windows. MSVC won't export the symbols until specified. I am aware about __declspec(dllexport). But the example provided in the CMake wiki is confusing. Please consider the following code.

#if defined (_WIN32) 
  #if defined(MyLibrary_EXPORTS)
    #define  MYLIB_EXPORT __declspec(dllexport)
  #else
    #define  MYLIB_EXPORT __declspec(dllimport)
  #endif /* MyLibrary_EXPORTS */
#else /* defined (_WIN32) */
 #define MYLIB_EXPORT
#endif

I understand __declspec(dllexport) but wondering why __declspec(dllimport) is used? Also how do I use this? Is it like MYLIB_EXPORT void function()?

Consider I have C function named foo(). This internally uses several static functions. When exporting do I need to export the static functions too开发者_运维问答? Or is it enough to export only the entry functions that are part of API?

Any help would be appreciated.


It is enough to export only the entry functions which are a part of API. No need to export static functions.

Also, no need to use __declspec(dllimport) for functions. It is needed only for data. Windows will automatically take care of doing the import incase of functions.

The below links can be helpful:

http://msdn.microsoft.com/en-us/library/ms235636(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ms682589(VS.85).aspx


That's typically used for a header file that's used both by your library to compile and by its clients; when you include the header in your library you define MyLibrary_EXPORTS and it will export the symbols, when you include the header in a client app it will import them instead.

No, you only need to export the API entry points - you don't need to export the static functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜