用于C函数定义的预处理器宏定义

3

I'm studying the SQLite source code (in c), but there are some things I can't figure out what they are, and I hope you can give me a hand. I know c, c++ and JAVA basics but I have never seen something like this and I don't know how to search for it.

In a c file, there are the next definitions (among others):

#ifndef SQLITE_CDECL
# define SQLITE_CDECL
#endif
#ifndef SQLITE_API
# define SQLITE_API
#endif

And then the above definitions are used like this:

SQLITE_API int SQLITE_CDECL sqlite3_conf(int, ...){
  //code

I know "SQLITE_API" and "SQLITE_CDECL" are not the return types, function names, keywords, K&R style nor variable modificators...

Why are this words placed like that in the function? What are they for?


1
在这个例子中很难看出来,因为SQLITE_CDECL和SQLITE_API似乎“什么也没做”。在某些平台上,可能需要“cdecl”来指定调用顺序。在其他平台上,可能需要“__declspec(dllimport)”来指定“.dll”与“.exe”的区别。以下是一个示例,其中Sqlite构建可能配置为给出SQLITE_CDECL和SQLITE_API有意义的非空值:__declspec(dllimport) void _cdecl func1(void); - paulsm4
2个回答

2

它们用于在不同平台上或以特定方式构建时修改函数的属性。大多数情况下可能不需要它们,但在某些情况下它们可能很有用,例如在构建DLL时使用__declspec(dllexport)或在直接包含文件而不是让链接器在目标文件之间执行链接时使用static


所以,如果我理解你的回答正确的话,这些单词可以被“静态”或空格(例如)替换? - algolejos
没错,另外还可以参考Gnu函数属性获取更多非Microsoft的示例。 - paulsm4

0

这两个声明看起来是为了支持在Windows上开发的某些怪癖而被定义得不同。请注意,在#define行中,它们没有被定义为任何值。这意味着它们将被替换为解析器看不到的空值。因此,在预处理器运行后,SQLITE_API int SQLITE_CDECL sqlite3_conf(int, ...){最终会被简化为int sqlite3_conf(int, ...){。在Windows上,它们可以被定义为不同的方式,以支持使用__declspec(参见https://msdn.microsoft.com/en-us/library/dabb5z75.aspx)构建DLL和指定调用约定(参见https://msdn.microsoft.com/en-us/library/zkwh89ks.aspx)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接