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

George picture George · Jun 1, 2009 · Viewed 10.1k times · Source

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

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

Answer

njsf picture njsf · Jun 1, 2009

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