What is this macro for at the beginning of a class definition?

前端 未结 3 826
無奈伤痛
無奈伤痛 2021-01-14 05:35

I\'m looking through the source of a library and many classes are defined using the following form

class THING_API ClassName 
{
...

Jumping

3条回答
  •  無奈伤痛
    2021-01-14 06:17

    It looks to me very much like export macro, which is required when building a shared library (.dll) on Windows. When compiling with MSVC, You have to put __declspec(export) in that spot when building a library, and __declspec(import) when building its client. This is achieved like so:

    #if COMPILING_DLL
        #define THING_API __declspec(dllexport)
    #else
        #define THING_API __declspec(dllimport)
    #endif
    

    Then you define COMPILING_DLL for the library project, and leave it undefined for all other projects. And if you're not on Windows or compiling a static library, you need to define it blank like it's done in your question.

    P. S. Other Windows compilers use their own keywords instead of __declspec(dllimport), but the principle remains.

提交回复
热议问题