开发者

Defining a Function in C

I am new to C and have a maybe n00b question ...

Until now I thought a function is always defined as:

return_type function_name(function_arg_1 type, function_arg_1_name, ...)

But now I have found a C header file 开发者_如何学运维where a function is looking like this:

TIDY_EXPORT int TIDY_CALL         tidyDetectedHtmlVersion( TidyDoc tdoc );

Which is more a:

i_do_not_know return_type i_do_not_know function_name(function_arg_1 type, function_arg_1_name)

The whole source can be seen at http://tidy.sourceforge.net/docs/api/tidy_8h-source.html

So what is "TIDY_EXPORT" and "TIDY_CALL" doing?


TIDY_EXPORT int TIDY_CALL are just modifiers to your function.

The function name is tidyDetectedHtmlVersion, it takes one parameter (tdoc) of type TidyDoc and returns an int.

TIDY_EXPORT is a macro defined in the platform.h file which defined as

00492 #if defined(_WIN32)
00493 
00494 #if (defined(_USRDLL) || defined(_WINDLL)) && !defined(TIDY_EXPORT)
00495 #define TIDY_EXPORT __declspec( dllexport ) 
00496 #endif

which allows you to export the function when compiling the file as a library (e.g. dll)

As for TIDY_CALL, it's defined in the same file as:

00498 #ifndef TIDY_CALL
00499 #ifdef _WIN64
00500 #  define TIDY_CALL __fastcall
00501 #else
00502 #  define TIDY_CALL __stdcall
00503 #endif
00504 #endif

See this question for an explanation What is __stdcall?


TIDY_EXPORT and TIDY_CALL are macros. They are defined in platform.h.


In platform.h you have

#if (defined(_USRDLL) || defined(_WINDLL)) && !defined(TIDY_EXPORT)
#define TIDY_EXPORT __declspec( dllexport )
#endif

#ifndef TIDY_CALL
#ifdef _WIN64
#define TIDY_CALL __fastcall
#else
# define TIDY_CALL __stdcall
#endif
#endif 


C is straightforward ... until macros get involved! Anything in ALL CAPS is probably a macro, which is a symbol which gets replaced with other text before compilation. That text can be defined to be anything, including an empty string. Luckily Doxygen has been used, so you can view the documentation http://tidy.sourceforge.net/docs/api/tidy_8h.html.

The file that is included:

#include "platform.h"

which you can read here, defines the macro.

For more info on macros in all their glory, read this.


TIDY_EXPORT is #defined as __declspec(dllexport) on Windows, to allow the function to be exported when Tidy is compiled as a DLL.

TIDY_CALL is a macro that expands to a platform-specific calling convention.

Both of these features are nonstandard extensions to the C language (which is why they are abstracted behind conditionally-compiled macros).


They are some macro defined somewere in your library. Do a text search in all the files and you probably finfd what they exactly defines. By using some paranormal diagnosys strategy I guess that TIDY export is some decoration to export the function from the library ( ie declspec(dllexport) in MS compilers, and TIDY_CALL specify the calling convention ( ie stdcall, pascal, and so on )


TIDY_CALL and so on are (It's my guess, but it's very probably) macros. http://www.google.pl/search?sourceid=chrome&ie=UTF-8&q=macros+C%2B%2B#sclient=psy&hl=pl&source=hp&q=macros+C&aq=f&aqi=&aql=&oq=&pbx=1&fp=58c095358c87453c


They are macros, you can find them in the source code somewhere. TIDY_CALL for example is in platform.h

 #ifndef TIDY_CALL
 #ifdef _WIN64
 #  define TIDY_CALL __fastcall
 #else
 #  define TIDY_CALL __stdcall
 #endif
 #endif
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜