对 GCC 的内置函数理解有困难,感到很困惑。
库函数和内置函数有什么区别?
有没有内置函数可以做而库函数不能做的事情?
我可以编写一个库函数来执行与内置函数 printf 相同的任务吗?如何判断输入参数的类型(%f、float 或 double)?
GCC 内置函数的机器指令不存储在库中,对吧?他们在哪里?
在做链接的时候,如何控制这些内置函数代码的放置位置?
为什么有时我在链接时会出现“未定义对 __builtin_stdarg_start 的引用”之类的错误消息
// main.c #include <stdio.h> int main(void) { printf("hello world!\n"); return 0; }
gcc -c main.c,nm显示main.o中没有符号printf,(只有main(T)和puts(U)),为什么?
最佳答案
What is the difference between a library function and an build-in function?
内置函数是编译器直接在编译器内部了解的函数。库函数只是在库中定义的函数。同名的内置函数和库函数可能同时存在,所以对于您的其余问题,我会将“库函数”视为“不是内置函数的库函数”。
Is there something a build-in function can do but a library function cannot?
是的。例如,内置函数可以选择不评估其参数:
int main() {
int i = 0;
__builtin_constant_p (++i); // checks whether ++i is a constant expression
// does not evaluate ++i
return i; // returns 0
}
这是因为编译器可以将内置函数转换为其他东西,实际上不需要包含任何函数调用。
Can I write a library function that performs the same task as the build in function printf?
有一些printf
的内置知识,但在大多数情况下,这是完全可行的。查看如何使用<stdarg.h>
.
How can I tell the type of the input parameters (%f, float or double)?
你必须信任调用者让格式字符串匹配剩余的参数;您无法检测到诸如传递 int
之类的东西当格式字符串需要 double
时.但是您不需要处理 float
之间的区别和 double
, 因为不可能通过 float
至printf
: 它将被转换为 double
(无论格式字符串如何)printf
之前看到它。 printf
的要求|已经过精心制作,以避免任何需要任何编译器魔法。
Machine instructions of GCC build-in functions are not stored in a library, right?
对内置函数的调用在编译时进行转换,但这种转换可能只是导致对同名库函数的调用。
Where are they?
如果转换是在编译时完成的,则没有机器指令。该调用被转换为不同的代码,然后该代码被编译以产生机器指令。如果结果是调用库函数,则该库函数的机器指令是库的一部分。
When doing linking, how can you control where to put these build-in function codes?
我不明白你在这里的意思。对内置函数的调用在编译时转换为不同的代码,然后将不同的代码编译为包含调用的函数的一部分。它将放置在该包含函数的其余代码将放置的任何位置。
Why sometimes I can error messages like "undefined reference to __builtin_stdarg_start" when doing linking
没有内置函数__builtin_stdarg_start
,尽管 __builtin
前缀,因此这被视为对库函数的调用。并且没有库函数__builtin_stdarg_start
要么,所以链接器将其检测为错误。
曾经有一个内置函数__builtin_stdarg_start
,但它在几年前就被删除了,代码从一开始就不应该使用它。
gcc -c main.c, nm shows that there is no symbol printf in main.o, (only main(T) and puts(U)) , why?
那是因为 printf
既作为内置函数又作为库函数存在。内置函数通常只是简单地调用库函数,但有时可以做得更好,包括在您的示例中。在这种情况下,内置函数 printf
无需调用库函数printf
即可给出正确结果.
关于c++ - GCC 如何处理内置函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23930588/