Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of declaration of function

I've been reading the stdio.h file and I'm confused.

For example, there is this line:

_CRTIMP FILE* __cdecl __MINGW_NOTHROW fopen (const char*, const char*);

I know FILE* means returning type and I found that _CRTIMP is constant, defined as

# ifndef _CRTIMP
#  define _CRTIMP  __declspec(dllimport)
# endif

I don't understand it, what is it there for? And what are rest of strings (__cdecl, __MINGW_NOTHROW) ?

like image 756
Buksy Avatar asked Feb 16 '11 12:02

Buksy


3 Answers

__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 #defined 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. :)

like image 99
Matteo Italia Avatar answered Sep 17 '22 14:09

Matteo Italia


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:

#define __cdecl  __attribute__ ((__cdecl__))
#define __MINGW_NOTHROW __attribute__ ((__nothrow__))

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).

like image 36
Christoph Avatar answered Sep 17 '22 14:09

Christoph


__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.

like image 24
unwind Avatar answered Sep 20 '22 14:09

unwind