3

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 PHPAPI do before the return type int? 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?

0x90
  • 39,472
  • 36
  • 165
  • 245
NONE
  • 471
  • 1
  • 4
  • 9
  • 1
    Most probably it specifies the calling convention. – Asha Apr 15 '13 at 06:35
  • 1
    PHPAPI is probably a #define in one of the include files. – John3136 Apr 15 '13 at 06:36
  • 1
    If you can pre-process the file then you can understand what the PHPAPI means – Raghuram Apr 15 '13 at 06:38
  • cc (or gcc) with the `-E` will show you the source file after the pre-processor has chewed on it. This is a quick (if verbose) way to find out what the macro expands to. – msw Apr 15 '13 at 06:40
  • You can search the PHP-Source code on GitHub: https://github.com/search?q=%22define+PHPAPI%22+%40php+repo%3Aphp%2Fphp-src&type=Code&ref=searchresults – MarcDefiant Apr 15 '13 at 06:41

1 Answers1

5

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 '\\'
#else
#define PHPAPI
#define THREAD_LS
#define PHP_DIR_SEPARATOR '/'
#endif

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
foo();

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

Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245