函数声明的含义
我一直在阅读 stdio.h
文件,但我很困惑。
例如,有这样一行:
_CRTIMP FILE* __cdecl __MINGW_NOTHROW fopen (const char*, const char*);
我知道 FILE* 表示返回类型,我发现 _CRTIMP 是常量,定义为
# ifndef _CRTIMP
# define _CRTIMP __declspec(dllimport)
# endif
我不明白,它是什么为了?其余字符串 (__cdecl
, __MINGW_NOTHROW
) 是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
__declspec(dllimport)
指定该函数将从单独的 dll 导入;我认为,根据 CRT 静态/动态链接设置,它的定义方式不同。__cdecl
是函数使用的调用约定;调用约定告诉编译器函数期望如何被调用(参数按什么顺序压入堆栈,是否使用任何寄存器作为参数,返回值存储在哪里,谁负责堆栈清理,... );一般来说,只要您不编写库,您就不应该担心它。__MINGW_NOTHROW
被#define
扩展为__attribute__ ((__nothro__))
,这是一个特定于 MinGW 的扩展,它告诉编译器函数不会抛出异常;这可以让编译器执行一些优化。请注意,所有这些都不是标准 C 属性,而是编译器/平台特定的东西。再说一次,一般来说你不应该担心它们,它们是使 CRT 正常工作所必需的,但只要你不构建库,你就可以在不了解它们的情况下逃脱。 <代码>:)
__declspec(dllimport)
specifies that the function is to be imported from a separate dll; I suppose that, depending on the CRT static/dynamic linking settings, it's defined in different ways.__cdecl
is the calling convention used for the function; the calling convention tells to the compiler how the function expects to be called (in which order parameters are pushed on the stack, if any register is used for parameters, where the return value is stored, who is responsible for stack cleanup, ...); in general you shouldn't worry about it as long as you're not writing libraries.__MINGW_NOTHROW
is#define
d to expand to__attribute__ ((__nothrow__))
, which is a MinGW-specific extension that tells to the compiler that the function will not throw exceptions; this lets the compiler perform some optimizations.Note that all these are not standard C attributes, but compiler/platform specific stuff. Again, in general you shouldn't worry about them, they are required to make the CRT work fine, but as long as you're not building libraries you can get away without knowing anything about them.
:)
您应该查看
_mingw.h
和 gcc 手册 :如果 gcc - 或任何其他支持__GNUC__
的编译器 - 以下定义适用:前者告诉编译器使用 cdecl x86 调用约定 (参见 gcc 手册),后者保证该函数不会抛出 C++ 异常(请参阅gcc手册)。
__declspec(dllimport)
对于动态链接工作是必要的 (参见 gcc 手册)。You should take a look at
_mingw.h
and the gcc manual: In case of gcc - or any other compiler supporting__GNUC__
- the following definitions apply:The former tells the compiler to use the cdecl x86 calling convention (see gcc manual), the latter that the function is guaranteed not to throw C++ exceptions (see gcc manual).
__declspec(dllimport)
is necessary to make dynamic linking work (see gcc manual).__declspec(dllimport)
告诉编译器该函数需要从 DLL 导入,它是 Windows 特定的扩展。有关详细信息,请参阅此页面。同样,__cdecl 是一个属性,指定该函数使用特定的调用约定(即 C 使用的调用约定)。请参阅此页面。
我猜想 __MINGW_NOTHROW 宏是 GCC nothrow 属性的同义词,它通知编译器相关函数不能抛出异常。有关详细信息,请参阅文档。
__declspec(dllimport)
tells the compiler that this function needs to be imported from a DLL, it's a Windows-specific extension. See this page for details.Likewise,
__cdecl
is an attribute that specifies that the function uses a particular calling convention (namely, the one used by C). See this page.I would guess that the __MINGW_NOTHROW macro is a synonym for the GCC
nothrow
attribute, which informs the compiler that the function in question cannot throw exceptions. See the documentation for details.这些是特定于您正在使用的环境或编译器的声明。
__declspec(dllimport)
表明,在 Windows 上,该函数位于 dll 中,并且应放置在生成的可执行 PE 文件的导入表中。__cdecl
指示标准 C 调用约定,并将修改编译器转换函数的方式以符合此调用约定。__MINGW_NOTHROW
特定于您的编译器,可能会停用对异常的支持。在任何情况下,编译器的文档中都必须有解释。
Theses are declarations specific to the environment or the compiler you're using.
__declspec(dllimport)
indicates, on windows, that this function is in a dll and should be placed in the import table of the executable PE file generated.__cdecl
indicate a standard C calling convention and will modify the way the compiler transform the function to conform to this calling convention.__MINGW_NOTHROW
is specific to your compiler and might de-activate support for exception.In all case, there must be an explanation in the documentation of your compiler.