C++ inline functions using GCC - why the CALL?

2019-02-02 08:27发布

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

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

Stroustrup in The C++ Programming language wirtes:

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.

alt text 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条回答
仙女界的扛把子
2楼-- · 2019-02-02 08:32

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

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

Example:

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

Source:GCC inline docs

查看更多
三岁会撩人
3楼-- · 2019-02-02 08:37

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

查看更多
\"骚年 ilove
4楼-- · 2019-02-02 08:39

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.

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.

查看更多
等我变得足够好
5楼-- · 2019-02-02 08:44

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...

查看更多
一纸荒年 Trace。
6楼-- · 2019-02-02 08:46

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).

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

查看更多
再贱就再见
7楼-- · 2019-02-02 08:47

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.

查看更多
登录 后发表回答