gpt4 book ai didi

c - 我的项目中包含哪些 libc,我可以省略它吗?

转载 作者:行者123 更新时间:2023-11-30 21:17:49 25 4
gpt4 key购买 nike

我有一个不包含在内的简单程序,编译时会出现一些警告:

int main(int argc, char **argv)
{
printf("hello");
exit(0);
}
<小时/>

编译:

gcc hello.c

Warning:
In function ‘main’:
warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
printf("hello");
^
warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
exit( 0 );
^

编译命令非常简单,并且不包含有关包含printfexit的包含库文件的信息。如果我错了,请纠正我,但看起来 gcc 默认将我的项目链接到这些库。这让我想到不需要包含 libc 库的文件?

再次纠正我。我收到警告是因为 GCC 以某种方式知道 libc 的一些基本函数,并且这些函数具有不同的参数。但由于 GCC 检查了所有 libc,它无论如何都找到了正确的函数。

libc到底是什么。它是一些标准的二进制对象和头文件集吗?

最佳答案

looks that gcc links my project to these libraries by default. That points me to thinking that include files of libc libraries is not required?

  • 包含和链接没有这种关系。这些 include 包含函数原型(prototype),这些函数原型(prototype)向编译器指示给定函数的返回值类型和参数。当函数调用发生时,并且没有原型(prototype),但编译器假定它返回 int,这可能会导致未定义的行为。

    程序将链接到标准库并且函数定义可用,但由于程序是在假设这些函数全部返回 int 的情况下编译的,因此可能会出现与此相关的运行时错误,这是无法预测的,因为行为是未定义

I got warnings because GCC somehow knows some basic functions of libc and these functions has different params. But since GCC checks all libc it founds correct function anyway.

  • 不,这与 gcc 知道任何事情无关,而是相反,它与 gcc 不知道如何调用这些函数有关。

What is libc at all. Is it some standard set of binary object and header files?

  • libc 是当前 glibc 中的二进制文件 libc.so.6,它是包含所需所有符号的运行时库按标准程序,它不包含例如 math.h 函数(即 libm.so.6)。

    您仍然需要 中的头文件出于上述原因的程序,或者至少是您使用的标准函数的声明。这些声明称为原型(prototype),编译器需要才能正确编译您的代码。

注意:始终至少使用 -Wall -Werror 进行编译,如下所示

gcc -Wall -Werror hello.c

关于c - 我的项目中包含哪些 libc,我可以省略它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com