C++ 什么是 __declspec,我什么时候需要使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2284610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is __declspec and when do I need to use it?
提问by Scott J
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?
我__declspec
在我正在阅读的代码中看到了 的实例。它是什么?我什么时候需要使用这个构造?
采纳答案by JaredPar
This is a Microsoft specific extension to the C++ language which allows you to attribute a type or function with storage class information.
这是 Microsoft 对 C++ 语言的特定扩展,它允许您使用存储类信息对类型或函数进行属性。
Documentation
文档
回答by Alexander Gessler
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.
规范示例是__declspec(dllimport)
and __declspec(dllexport)
,它们指示链接器从或向 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)
(__declspec(..)
只是包装了微软的特定东西 - 为了实现兼容性,人们通常会用宏包装它)
回答by AndiDog
It is mostly used for importing symbols from / exporting symbols to a shared library (DLL). Both Visual C++ and GCC compilers support __declspec(dllimport)
and __declspec(dllexport)
. Other uses (some Microsoft-only) are documented in the MSDN.
它主要用于从符号导入/导出符号到共享库 (DLL)。Visual C++ 和 GCC 编译器都支持__declspec(dllimport)
和__declspec(dllexport)
. MSDN中记录了其他用途(一些仅限 Microsoft)。
回答by HackNone
Another example to illustrate the __declspeckeyword:
另一个说明__declspec关键字的示例:
When you are writing a Windows Kernel Driver, sometimes you want to write your own prolog/epilog code sequences using inline assembler code, so you could declare your function with the nakedattribute.
当您编写 Windows 内核驱动程序时,有时您想使用内联汇编代码编写自己的 prolog/epilog 代码序列,因此您可以使用裸属性声明您的函数。
__declspec( naked ) int func( formal_parameters ) {}
Or
或者
#define Naked __declspec( naked )
Naked int func( formal_parameters ) {}
Please refer to naked (C++)
请参考裸 (C++)
回答by Seva Alekseyev
Essentially, it's the way Microsoft introduces its C++ extensions so that they won't conflict with future extensions of standard C++. With __declspec, you can attribute a function or class; the exact meaning varies depending on the nature of __declspec. __declspec(naked), for example, suppresses prolog/epilog generation (for interrupt handlers, embeddable code, etc), __declspec(thread) makes a variable thread-local, and so on.
本质上,这是 Microsoft 引入其 C++ 扩展的方式,因此它们不会与标准 C++ 的未来扩展发生冲突。使用 __declspec,您可以对函数或类进行属性;确切含义因 __declspec 的性质而异。例如,__declspec(naked) 抑制 prolog/epilog 生成(用于中断处理程序、可嵌入代码等),__declspec(thread) 使变量线程局部,等等。
The full list of __declspec attributes is available on MSDN, and varies by compiler version and platform.
__declspec 属性的完整列表可在 MSDN 上找到,并且因编译器版本和平台而异。
回答by Seva Alekseyev
I know it's been eight years but I wanted to share this piece of code found in MRuby that shows how __declspec()
can bee used at the same level as the export keyword
.
我知道已经八年了,但我想分享在 MRuby 中找到的这段代码,它展示了如何__declspec()
在与export keyword
.
/** Declare a public MRuby API function. */
#if defined(MRB_BUILD_AS_DLL)
#if defined(MRB_CORE) || defined(MRB_LIB)
# define MRB_API __declspec(dllexport)
#else
# define MRB_API __declspec(dllimport)
#endif
#else
# define MRB_API extern
#endif