Buksy
Buksy

Reputation: 12238

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

Upvotes: 8

Views: 5744

Answers (4)

Christoph
Christoph

Reputation: 169833

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

Upvotes: 5

Matteo Italia
Matteo Italia

Reputation: 126967

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

Upvotes: 10

Nekresh
Nekresh

Reputation: 2988

Theses are declarations specific to the environment or the compiler you're using.

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

Upvotes: 0

unwind
unwind

Reputation: 400159

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

Upvotes: 1

Related Questions