NONE NONE - 7 months ago 29
Linux Question

weird c function syntax

I know my C up to this point. I was looking at the source files of PHP I downloaded, and I saw this strange syntax:

PHPAPI int php_printf(const char *format, ...)
// code...

What does the
do before the return type
? I've tried searching all over and I can't understand what this means. Is it a second return type? It can't be because the function does return an int. Maybe it extends to some other struct declared in a header file?


The hard way:

Go to the makefile and add in the line that compiles the sources: -E, by doing so you will see the source cose after the preprocessing phase.

The easy way:

Search all the project for PHPAPI:

find it in php.h:

#ifdef PHP_WIN32
#include "win95nt.h"
#   ifdef PHP_EXPORTS
#   define PHPAPI __declspec(dllexport) 
#   else
#   define PHPAPI __declspec(dllimport) 
#   endif
#define PHP_DIR_SEPARATOR '\\'
#define PHPAPI
#define THREAD_LS

Now what you need to know is what is __declspec(dllexport) and what is __declspec(dllimport)

In the SO thread- What is __declspec and when do I need to use it?

see Alexander Gessler answer:

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.

// header
__declspec(dllimport) void foo();

// code - this calls foo() somewhere in a DLL

(__declspec(..) just wraps up Microsoft's specific stuff - to achieve compatibility, one would usually wrap it away with macros)