What is __declspec and when do I need to use it?

前端 未结 6 1180
耶瑟儿~
耶瑟儿~ 2020-11-30 18:08

I have seen instances of __declspec in the code that I am reading. What is it? And when would I need to use this construct?

6条回答
  •  一生所求
    2020-11-30 18:16

    The canonical examples are __declspec(dllimport) and __declspec(dllexport), which instruct the linker to import and export (respectively) a symbol from or to a DLL.

    // header
    __declspec(dllimport) void foo();
    
    
    // code - this calls foo() somewhere in a DLL
    foo();
    

    (__declspec(..) just wraps up Microsoft's specific stuff - to achieve compatibility, one would usually wrap it away with macros)

提交回复
热议问题