C/C++函数的定义

发布于 2024-08-25 07:28:04 字数 145 浏览 6 评论 0原文

昨天,我一直在这里观看有关编译器和链接器的讨论。这是关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸印温柔 2024-09-01 07:28:04

printf() 的 C99 声明是

int printf(const char *restrict, ...);

,但大多数编译器也会接受 另

int printf(const char *, ...);    

请参阅 C99 第 7.1.4 节,§2:

假设可以在不引用库函数中定义的任何类型的情况下声明库函数
标头,也可以声明函数并使用它而不包含它的
关联的标头。

注意:在这种情况下,restrict 限定符与 const 相结合,向编译器保证格式字符串永远不会在 printf() 中修改,即使指针作为可变参数之一再次传递。

The C99 declaration of printf() is

int printf(const char *restrict, ...);

but most compilers will also accept

int printf(const char *, ...);    

See also C99 section 7.1.4, §2:

Provided that a library function can be declared without reference to any type defined in a
header, it is also permissible to declare the function and use it without including its
associated header.

Note: In this case, the restrict qualifier combined with const promises the compiler that the format string is never modified within printf(), even if the pointer is passed again as one of the variadic arguments.

有深☉意 2024-09-01 07:28:04

该定义通常编译在共享库中。声明就是您所需要的。范围内没有声明会调用未定义的行为。因此,对于每个库,您通常都会有一个(一组)头文件和已编译的二进制共享/静态库。您可以通过包含适当的标头并与库链接来编译源代码。要将声明引入作用域,请使用#include 指令。例如,对于 printf 你会这样做:

#include <stdio.h>
int main() {
    printf("Hello, world\n");
    return 0;
}

但是任何关于 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. for printf you'd do:

#include <stdio.h>
int main() {
    printf("Hello, world\n");
    return 0;
}

But then any decent book on C or C++ should already cover this in detail and with better examples.

却一份温柔 2024-09-01 07:28:04

这取决于您的编译器和平台。

在大多数情况下,只需声明

int printf(const char *, ...);

即可,但是,出于调用约定的目的,您的特定编译器/平台或 C 库实现甚至可以更改此声明。

总而言之,不值得尝试自己声明事物,因为这可能违反单一定义规则。在这种情况下,您应该始终包含适当的头文件 stdio.h(cstdio for C++)。

It depends on your compiler and platform.

On most cases just declaring

int printf(const char *, ...);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文