C/C++函数的定义
昨天,我一直在这里观看有关编译器和链接器的讨论。这是关于 C 库函数定义的。我从来没有想过这一点,所以它启发我做了一些搜索,但我找不到我想要的东西。我想知道,您需要添加到源代码中以启用 printf() 函数的最小语法是什么。我的意思是您需要的 stdio.h 中的函数声明。
Yesterday, I have been watching discussion here, about compilers and linkers. It was about C library function definitions. I have never thought of that, so it inspired me to do some searching, but I cannot find exactly what I want. I wonder, what is the smallest syntax you need to add into your source code to enable just printf() function. I mean the function declaration from stdio.h you need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
printf()
的 C99 声明是,但大多数编译器也会接受 另
请参阅 C99 第 7.1.4 节,§2:
注意:在这种情况下,
restrict
限定符与const
相结合,向编译器保证格式字符串永远不会在printf() 中修改
,即使指针作为可变参数之一再次传递。The C99 declaration of
printf()
isbut most compilers will also accept
See also C99 section 7.1.4, §2:
Note: In this case, the
restrict
qualifier combined withconst
promises the compiler that the format string is never modified withinprintf()
, even if the pointer is passed again as one of the variadic arguments.该定义通常编译在共享库中。声明就是您所需要的。范围内没有声明会调用未定义的行为。因此,对于每个库,您通常都会有一个(一组)头文件和已编译的二进制共享/静态库。您可以通过包含适当的标头并与库链接来编译源代码。要将声明引入作用域,请使用
#include
指令。例如,对于printf
你会这样做:但是任何关于 C 或 C++ 的像样的书都应该已经详细介绍了这一点并提供了更好的示例。
The definition is usually compiled in a shared library. The declaration is what you need. Not having a declaration in scope invokes undefined behavior. So, for every library, you'd typically have a (set of) header file(s) and the compiled binary shared/static library. You compile your sources by including appropriate headers and link with the library. To bring in the declaration in scope use the
#include
directive. E.g. forprintf
you'd do:But then any decent book on C or C++ should already cover this in detail and with better examples.
这取决于您的编译器和平台。
在大多数情况下,只需声明
即可,但是,出于调用约定的目的,您的特定编译器/平台或 C 库实现甚至可以更改此声明。
总而言之,不值得尝试自己声明事物,因为这可能违反单一定义规则。在这种情况下,您应该始终包含适当的头文件 stdio.h(cstdio for C++)。
It depends on your compiler and platform.
On most cases just declaring
will just do, however, your particular compiler/platform or C library implementation even can change this declaration, for calling convention purposes.
All in all it is not worth it to try and declare things yourself, as this could be a violation of the one definition rule. You should always include the apropriate header, stdio.h(cstdio for C++) in this case.