使用GCC的C ++内联函数 - 为什么要调用CALL?

时间:2022-01-27 03:10:10

I have been testing inline function calls in C++.

我一直在用C ++测试内联函数调用。

Thread model: win32
gcc version 4.3.3 (4.3.3-tdm-1 mingw32)

Stroustrup in The C++ Programming language wirtes:

C ++编程语言中的Stroustrup:

The inline specifier is a hint to the compiler that it should attempt to generate code [...] inline rather than laying down the code for the function once and then calling through the usual function call mechanism.

内联说明符是编译器的一个提示,它应该尝试生成内联代码而不是为函数设置一次代码,然后通过通常的函数调用机制调用。

However, I have found out that the generated code is simply not inline. There is a CALL instrction for the isquare function.

但是,我发现生成的代码根本不是内联的。 isquare函数有一个CALL指令。

alt text http://i42.tinypic.com/8ys3f4.jpg

替代文字http://i42.tinypic.com/8ys3f4.jpg

Why is this happening? How can I use inline functions then?

为什么会这样?那我该如何使用内联函数呢?

EDIT: The command line options used:

编辑:使用的命令行选项:

**** Build of configuration Debug for project InlineCpp ****

**** Internal Builder is used for build               ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\InlineCpp.o ..\src\InlineCpp.cpp
g++ -oInlineCpp.exe src\InlineCpp.o

7 个解决方案

#1


22  

There is no generic C++ way to FORCE the compiler to create inline functions. Note the word 'hint' in the text you quoted - the compiler is not obliged to listen to you.

没有通用的C ++方法来强制编译器创建内联函数。请注意您引用的文本中的“提示”一词 - 编译器没有义务听取您的意见。

If you really, absolutely have to make something be in-line, you'll need a compiler specific keyword, OR you'll need to use macros instead of functions.

如果你真的,绝对要做一些内联,你需要一个特定于编译器的关键字,或者你需要使用宏而不是函数。

EDIT: njsf gives the proper gcc keyword in his response.

编辑:njsf在他的回复中给出了正确的gcc关键字。

#2


47  

Like Michael Kohne mentioned, the inline keyword is always a hint, and GCC in the case of your function decided not to inline it.

就像Michael Kohne所提到的那样,inline关键字总是一个提示,而在你的函数的情况下,GCC决定不内联它。

Since you are using Gcc you can force inline with the __attribute((always_inline)).

由于您使用的是Gcc,因此可以强制使用__attribute((always_inline))进行内联。

Example:

 /* Prototype.  */
 inline void foo (const char) __attribute__((always_inline));

Source:GCC inline docs

资料来源:GCC内联文档

#3


8  

Are you looking at a debug build (optimizations disabled)? Compilers usually disable inlining in "debug" builds because they make debugging harder.

您是否正在查看调试版本(已禁用优化)?编译器通常会在“调试”版本中禁用内联,因为它们会使调试更加困难。

In any case, the inline specified is indeed a hint. The compiler is not required to inline the function. There are a number of reasons why any compiler might decide to ignore an inline hint:

在任何情况下,指定的内联确实是一个提示。编译器不需要内联函数。任何编译器可能决定忽略内联提示的原因有很多:

  • A compiler might be simple, and not support inlining
  • 编译器可能很简单,不支持内联

  • A compiler might use an internal algorithm to decide on what to inline and ignore the hints.
    (sometimes, the compiler can do a better job than you can possibly do at choosing what to inline, especially in complex architectures like IA64)
  • 编译器可能使用内部算法来决定内联和忽略提示的内容。 (有时,编译器可以比选择内联的内容做得更好,特别是在像IA64这样的复杂架构中)

  • A compiler might use its own heuristics to decide that despite the hint, inlining will not improve performance
  • 编译器可能会使用自己的启发式方法来确定尽管有提示,但内联不会提高性能

#4


4  

Inline is nothing more than a suggestion to the compiler that, if it's possible to inline this function then the compiler should consider doing so. Some functions it will inline automatically because they are so simple, and other functions that you suggest it inlines it won't because they are to complex.

内联只不过是对编译器的一个建议,如果可以内联这个函数,那么编译器应该考虑这样做。有些函数会自动内联,因为它们非常简单,而你建议它的其他函数也不会内联,因为它们很复杂。

Also, I noticed that you are doing a debug build. I don't actually know, but it's possible that the compiler disables inlining for debug builds because it makes things difficult for the debugger...

另外,我注意到你正在进行调试构建。我实际上并不知道,但编译器可能会禁用调试版本的内联,因为这会使调试器变得困难......

#5


3  

It is a hint and the complier can choice to ignore the hint. I think I read some where that GCC generally ignore it. I remeber hearing there was a flag but it still does not work in 100% of cases. (I have not found a link yet).

这是一个提示,编译器可以选择忽略提示。我想我读过GCC一般忽略它的地方。我记得听说有一面旗帜,但在100%的情况下仍然不起作用。 (我还没有找到链接)。

Flag: -finline-functions is turned on at -O3 optimisation level.

标志:-finline-functions在-O3优化级别打开。

#6


0  

Whether to inline is up to the compiler. Is it free to ignore the inline hint. Some compilers have a specific keyword (like __forceinline in VC++) but even with such a keyword virtual calls to virtual member functions will not be inlined.

是否内联取决于编译器。是否可以忽略内联提示。有些编译器有一个特定的关键字(如VC ++中的__forceinline),但即使使用这样的关键字,虚拟成员函数的虚拟调用也不会被内联。

#7


0  

I faced similar problems and found that it only works if the inline function is written in a header file.

我遇到了类似的问题,发现只有在内联函数写入头文件时它才有效。

#1


22  

There is no generic C++ way to FORCE the compiler to create inline functions. Note the word 'hint' in the text you quoted - the compiler is not obliged to listen to you.

没有通用的C ++方法来强制编译器创建内联函数。请注意您引用的文本中的“提示”一词 - 编译器没有义务听取您的意见。

If you really, absolutely have to make something be in-line, you'll need a compiler specific keyword, OR you'll need to use macros instead of functions.

如果你真的,绝对要做一些内联,你需要一个特定于编译器的关键字,或者你需要使用宏而不是函数。

EDIT: njsf gives the proper gcc keyword in his response.

编辑:njsf在他的回复中给出了正确的gcc关键字。

#2


47  

Like Michael Kohne mentioned, the inline keyword is always a hint, and GCC in the case of your function decided not to inline it.

就像Michael Kohne所提到的那样,inline关键字总是一个提示,而在你的函数的情况下,GCC决定不内联它。

Since you are using Gcc you can force inline with the __attribute((always_inline)).

由于您使用的是Gcc,因此可以强制使用__attribute((always_inline))进行内联。

Example:

 /* Prototype.  */
 inline void foo (const char) __attribute__((always_inline));

Source:GCC inline docs

资料来源:GCC内联文档

#3


8  

Are you looking at a debug build (optimizations disabled)? Compilers usually disable inlining in "debug" builds because they make debugging harder.

您是否正在查看调试版本(已禁用优化)?编译器通常会在“调试”版本中禁用内联,因为它们会使调试更加困难。

In any case, the inline specified is indeed a hint. The compiler is not required to inline the function. There are a number of reasons why any compiler might decide to ignore an inline hint:

在任何情况下,指定的内联确实是一个提示。编译器不需要内联函数。任何编译器可能决定忽略内联提示的原因有很多:

  • A compiler might be simple, and not support inlining
  • 编译器可能很简单,不支持内联

  • A compiler might use an internal algorithm to decide on what to inline and ignore the hints.
    (sometimes, the compiler can do a better job than you can possibly do at choosing what to inline, especially in complex architectures like IA64)
  • 编译器可能使用内部算法来决定内联和忽略提示的内容。 (有时,编译器可以比选择内联的内容做得更好,特别是在像IA64这样的复杂架构中)

  • A compiler might use its own heuristics to decide that despite the hint, inlining will not improve performance
  • 编译器可能会使用自己的启发式方法来确定尽管有提示,但内联不会提高性能

#4


4  

Inline is nothing more than a suggestion to the compiler that, if it's possible to inline this function then the compiler should consider doing so. Some functions it will inline automatically because they are so simple, and other functions that you suggest it inlines it won't because they are to complex.

内联只不过是对编译器的一个建议,如果可以内联这个函数,那么编译器应该考虑这样做。有些函数会自动内联,因为它们非常简单,而你建议它的其他函数也不会内联,因为它们很复杂。

Also, I noticed that you are doing a debug build. I don't actually know, but it's possible that the compiler disables inlining for debug builds because it makes things difficult for the debugger...

另外,我注意到你正在进行调试构建。我实际上并不知道,但编译器可能会禁用调试版本的内联,因为这会使调试器变得困难......

#5


3  

It is a hint and the complier can choice to ignore the hint. I think I read some where that GCC generally ignore it. I remeber hearing there was a flag but it still does not work in 100% of cases. (I have not found a link yet).

这是一个提示,编译器可以选择忽略提示。我想我读过GCC一般忽略它的地方。我记得听说有一面旗帜,但在100%的情况下仍然不起作用。 (我还没有找到链接)。

Flag: -finline-functions is turned on at -O3 optimisation level.

标志:-finline-functions在-O3优化级别打开。

#6


0  

Whether to inline is up to the compiler. Is it free to ignore the inline hint. Some compilers have a specific keyword (like __forceinline in VC++) but even with such a keyword virtual calls to virtual member functions will not be inlined.

是否内联取决于编译器。是否可以忽略内联提示。有些编译器有一个特定的关键字(如VC ++中的__forceinline),但即使使用这样的关键字,虚拟成员函数的虚拟调用也不会被内联。

#7


0  

I faced similar problems and found that it only works if the inline function is written in a header file.

我遇到了类似的问题,发现只有在内联函数写入头文件时它才有效。