GCC如何处理内置功能 [英] How GCC handles built-in function

查看:72
本文介绍了GCC如何处理内置功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解GCC内置功能时遇到麻烦,感到非常困惑.

I have trouble understanding GCC built-in functions, and feel very confused.

  • 库函数和内置函数有什么区别?

  • What is the difference between a library function and an built-in function?

有内置函数可以执行但库函数不能执行的操作吗?

Is there something a built-in function can do but a library function cannot?

我可以编写一个执行与内置函数printf相同的任务的库函数吗?如何分辨输入参数的类型(%f,float或double)?

Can I write a library function that performs the same task as the built-in function printf? How can I tell the type of the input parameters (%f, float or double)?

GCC内置函数的机器指令未存储在库中,对吗?他们在哪里?

Machine instructions of GCC built-in functions are not stored in a library, right? Where are they?

进行链接时,如何控制将这些内置功能代码放置在何处?

When doing linking, how can you control where to put these built-in function codes?

为什么有时在链接时会出现错误消息,例如对__builtin_stdarg_start的未定义引用"

Why sometimes I can error messages like "undefined reference to __builtin_stdarg_start" when doing linking

// 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)),为什么?

gcc -c main.c, nm shows that there is no symbol printf in main.o, (only main(T) and puts(U)) , why?

推荐答案

库函数和内置函数有什么区别?

What is the difference between a library function and an build-in function?

内置函数是编译器直接在编译器内部具有一些知识的函数.库函数只是库中定义的一个函数.内置函数和同名库函数都可能同时存在,因此对于其余的问题,我将把库函数"视为不是内置函数的库函数".

A built-in function is one that the compiler has some knowledge of directly inside the compiler itself. A library function is simply one defined in a library. A built-in function and a library function of the same name may both exist, so for the rest of your questions, I will treat "library function" as "library function that is not a built-in function".

有内置函数可以执行但库函数不能执行的操作吗?

Is there something a build-in function can do but a library function cannot?

是的.内置函数可能会选择不评估其参数,例如:

Yes. A built-in function may choose, for example, not to evaluate its arguments:

int main() {
  int i = 0;
  __builtin_constant_p (++i); // checks whether ++i is a constant expression
                              // does not evaluate ++i
  return i; // returns 0
}

这是因为编译器可以将内置函数转换为其他函数,而实际上不需要包含任何函数调用.

This is because a built-in function can be transformed by the compiler into something else, that does not actually need to contain any function call.

我可以编写一个库函数来执行与内置函数printf相同的任务吗?

Can I write a library function that performs the same task as the build in function printf?

有一些printf的内置知识,但是在大多数情况下,这是完全可行的.查找如何使用<stdarg.h>.

There is some built-in knowledge of printf, but for the most part, this is perfectly doable. Look up how to use <stdarg.h>.

如何知道输入参数的类型(%f,float或double)?

How can I tell the type of the input parameters (%f, float or double)?

您必须信任调用方以使格式字符串与其余参数匹配;当格式字符串需要double时,您将无法检测到类似通过int的操作.但是您无需处理floatdouble之间的区别,因为不可能将float传递给printf:在printf看到它. printf的要求已经精心制定,以避免需要任何编译器魔术.

You have to trust the caller to let the format string match the remaining arguments; you cannot detect something like passing an int when the format string expects a double. But you don't need to handle the difference between float and double, because it's impossible to pass a float to printf: it will be converted to double (regardless of the format string) before printf sees it. The requirements of printf have been carefully made to avoid any need for any compiler magic.

GCC内置函数的机器指令没有存储在库中,对吧?

Machine instructions of GCC build-in functions are not stored in a library, right?

对内置函数的调用在编译时进行转换,但是这种转换可能只是导致对同名库函数的调用.

Calls to built-in functions are transformed at compile time, but that transformation may be simply result in a call to a library function of the same name.

他们在哪里?

Where are they?

如果转换是在编译时完成的,则没有机器指令.该调用将转换为不同的代码,然后对该代码进行编译以生成机器指令.如果结果是对库函数的调用,则该库函数的机器指令是库的一部分.

If the transformation is done at compile time, there are no machine instructions. The call is transformed into different code, and that code is then compiled to produce machine instructions. If the result is a call to a library function, the machine instructions for that library function are part of the library.

进行链接时,如何控制这些内置功能代码的放置位置?

When doing linking, how can you control where to put these build-in function codes?

我不明白你在这里的意思.对内置函数的调用在编译时转换为不同的代码,然后将该不同的代码编译为包含该调用的函数的一部分.它将放置在该包含函数的其余代码将放置在的任何地方.

I don't understand what you mean here. A call to a built-in function is transformed at compile time to different code, and that different code is then compiled as part of the function containing the call. It will be put wherever the rest of the code of that containing function will be put.

为什么有时在链接时会出现错误消息,例如对__builtin_stdarg_start的未定义引用"

Why sometimes I can error messages like "undefined reference to __builtin_stdarg_start" when doing linking

尽管前缀为__builtin,但没有内置函数__builtin_stdarg_start,因此将其视为对库函数的调用.而且也没有库函数__builtin_stdarg_start,因此链接程序将其检测为错误.

There is no built-in function __builtin_stdarg_start, despite the __builtin prefix, so this is treated as a call to a library function. And there is no library function __builtin_stdarg_start either, so the linker detects this as an error.

曾经有一个内置函数__builtin_stdarg_start,但几年前已被删除,并且代码从不应该一开始就使用它.

There used to be a built-in function __builtin_stdarg_start, but it was removed years ago, and code never should have been using it in the first place.

gcc -c main.c,nm表明main.o中没有符号printf(仅main(T)和puts(U)),为什么?

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.

That's because printf exists both as a built-in function and as a library function. The built-in function usually simply calls the library function, but sometimes it is possible to do better, including in your example. In this case, the built-in function printf can give the correct result without calling the library function printf.

这篇关于GCC如何处理内置功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆