user13188950
user13188950

Reputation:

Using a macro in function declaration in C

I have come across this code in the source of CMake:

https://fossies.org/windows/misc/cmake-3.17.0.zip/cmake-3.17.0/Utilities/cmzlib/compress.c

int ZEXPORT compress (dest, destLen, source, sourceLen)
  Bytef *dest;
  uLongf *destLen;
  const Bytef *source;
  uLong sourceLen;
{
  return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
}

Why is ZEXPORT used in the function, and how does it even compile?

If I change ZEXPORT to a random integer, like 5:

int 5 compress (dest, destLen, source, sourceLen)

code won't even compile anymore.

Here are possible expansions:

define ZEXPORT WINAPI
define ZEXPORT   __declspec(dllexport)

Upvotes: 0

Views: 127

Answers (1)

S.S. Anne
S.S. Anne

Reputation: 15566

If Windows is used, this macro compiles to:

WINAPI

This is another macro that most likely expands to:

__stdcall

This makes the Microsoft compiler use a calling convention where the callee (rather than the caller) cleans up the stack.

On BEOS, this is defined to either:

__declspec(dllimport)

or:

__declspec(dllexport)

depending on whether the header file is being used in a user application or the library itself, respectively.

If any other operating system other than Windows or BEOS is used, then this macro is defined to nothing.

Upvotes: 2

Related Questions