inline functions not inlined

  • Thread starter Bilgehan.Balban
  • Start date
B

Bilgehan.Balban

Hi,

If I define an inline function in one .c file, and use it from another,
after compiling and linking the two, it seems the function is not
inlined but rather called as a regular function. I would expect to see
it inlined during linkage of the two object files. Does inlining only
occur if the inline function is defined within the same file that it is
called?

Thanks,
Bahadir
 
K

Keith Thompson

If I define an inline function in one .c file, and use it from another,
after compiling and linking the two, it seems the function is not
inlined but rather called as a regular function. I would expect to see
it inlined during linkage of the two object files. Does inlining only
occur if the inline function is defined within the same file that it is
called?

C99 6.7.4p5:

A function declared with an inline function specifier is an
_inline function_. The function specifier may appear more than
once; the behavior is the same as if it appeared only once. Making
a function an inline function suggests that calls to the function
be as fast as possible. The extent to which such suggestions are
effective is implementation-defined.

So an implementation isn't required to do anything special with an
inline function (it's like "register" in that sense).

Also, C99 6.7.4p6:

Any function with internal linkage can be an inline function. For
a function with external linkage, the following restrictions
apply: If a function is declared with an inline function
specifier, then it shall also be defined in the same translation
unit. If all of the file scope declarations for a function in a
translation unit include the inline function specifier without
extern, then the definition in that translation unit is an _inline
definition_. An inline definition does not provide an external
definition for the function, and does not forbid an external
definition in another translation unit. An inline definition
provides an alternative to an external definition, which a
translator may use to implement any call to the function in the
same translation unit. It is unspecified whether a call to the
function uses the inline definition or the external definition.

So this is more complicated than I thought it was. Perhaps some day
I'll sit down and figure out out; in the meantime, you have the
standard's wording to contemplate.
 
I

Ian Collins

Keith said:
C99 6.7.4p5:

A function declared with an inline function specifier is an
_inline function_. The function specifier may appear more than
once; the behavior is the same as if it appeared only once. Making
a function an inline function suggests that calls to the function
be as fast as possible. The extent to which such suggestions are
effective is implementation-defined.

So an implementation isn't required to do anything special with an
inline function (it's like "register" in that sense).
Maybe it's an added complexity like 'register' which the language would
be better off without. A hangover from older compilers with poor
optimisation.

Let the optimiser to the work.

Ian
 
M

Micah Cowan

Hi,

If I define an inline function in one .c file, and use it from another,
after compiling and linking the two, it seems the function is not
inlined but rather called as a regular function. I would expect to see
it inlined during linkage of the two object files. Does inlining only
occur if the inline function is defined within the same file that it is
called?

This is essentially correct. It will definitely not occur during
linkage. In fact, depending on how it was declared, it may not link
/externally/ at all.
 
K

Keith Thompson

Ian Collins said:
Maybe it's an added complexity like 'register' which the language
would be better off without. A hangover from older compilers with
poor optimisation.

Let the optimiser to the work.

I suspect that modern compilers aren't as good at automatically
inlining functions as they are at assigning variables to registers,
and that user-controlled inlining still makes sense. (I have no hard
data to back that up.)

Providing inline could also tend to discourage the somewhat dangerous
use of macros as "faster" functions.
 
I

Ian Collins

Keith said:
I suspect that modern compilers aren't as good at automatically
inlining functions as they are at assigning variables to registers,
and that user-controlled inlining still makes sense. (I have no hard
data to back that up.)
Modern C++ compilers (at least the Sun compilers I use) tend to be
strong in this area, so I'd hope C compilers from the same vendor would
be as well. A couple of quick tests show the same code generated, with
appropriate inline optimisation, by both the C and C++ compilers.
Providing inline could also tend to discourage the somewhat dangerous
use of macros as "faster" functions.
Agreed, maybe the need will fade as programmers become used to compilers
inlining functions. C++ programmers hardly ever (if they value their
job!) use macros for 'quick' functions, they are used to the compiler
doing the job for them.
 
R

Rod Pemberton

Micah Cowan said:
This is essentially correct. It will definitely not occur during
linkage. In fact, depending on how it was declared, it may not link
/externally/ at all.

My experience has been that the function will only be inlined in the file in
which it is declared. Some compilers, will generate a callable non-inline
version of the function (GCC) which can be called from other files and other
compilers will not generate a non-inline version of the
function(OpenWatcom).


Rod Pemberton
 
B

Bilgehan.Balban

Keith said:
C99 6.7.4p5:

A function declared with an inline function specifier is an
_inline function_. The function specifier may appear more than
once; the behavior is the same as if it appeared only once. Making
a function an inline function suggests that calls to the function
be as fast as possible. The extent to which such suggestions are
effective is implementation-defined.

So an implementation isn't required to do anything special with an
inline function (it's like "register" in that sense).

Thanks for the reply. I guess it's never guaranteed by the language
that your function will be inlined.
Also, C99 6.7.4p6:

Any function with internal linkage can be an inline function. For
a function with external linkage, the following restrictions
apply: If a function is declared with an inline function
specifier, then it shall also be defined in the same translation
unit. If all of the file scope declarations for a function in a
translation unit include the inline function specifier without
extern, then the definition in that translation unit is an _inline
definition_. An inline definition does not provide an external
definition for the function, and does not forbid an external
definition in another translation unit. An inline definition
provides an alternative to an external definition, which a
translator may use to implement any call to the function in the
same translation unit. It is unspecified whether a call to the
function uses the inline definition or the external definition.

So this is more complicated than I thought it was. Perhaps some day
I'll sit down and figure out out; in the meantime, you have the
standard's wording to contemplate.

I guess this refers to `inline' functions with `extern' qualifier. I
think as a rule of thumb, it means that you can declare multiple copies
of a function this way in multiple translation units and the compiler
will be free to choose any of them, but with a better chance to choose
the local inline copy of it in the same translation unit where it is
used.

My original intention was to find out a way to produce inlined code
even though the functions were defined in different translation units.
My personal conclusion is that it's not possible unless the function is
defined in the same translation unit where it is used. I think the only
way to do it is define inline functions in header files, #include them,
and hope for them to be inlined by the compiler.

Bahadir
 
R

Robin Haigh

Hi,

If I define an inline function in one .c file, and use it from another,
after compiling and linking the two, it seems the function is not
inlined but rather called as a regular function. I would expect to see
it inlined during linkage of the two object files. Does inlining only
occur if the inline function is defined within the same file that it is
called?


If you haven't declared the function inline before you call it, there's no
suggestion that it should be inlined (the compiler isn't going to look in a
different translation unit).

If you have declared the function inline, there should be a definition, i.e.
a function body, in the same translation unit (source file and included
headers), or the behaviour is undefined.

If the function is declared static, then everything is easy. Note that you
can put a static function definition in a header file. This is the way to
go.

Otherwise, things get very messy, because you have to organise visible
definitions wherever needed without creating multiple external definitions.

So far as I can see, the ways to use non-static inline functions with a
normal "modular" program structure are

(a) inline definition in a header: one source file must also include an
extern or non-inline declaration, to create an external definition. The
compiler may use either definition

(b) non-inline declaration in a header: one source file must provide the
definition, as usual. If the inline keyword is used here, or in a "forward
declaration" within the source file, this is simply a hint that subsequent
calls in the same file be inlined.
 
D

David Resnick

My original intention was to find out a way to produce inlined code
even though the functions were defined in different translation units.
My personal conclusion is that it's not possible unless the function is
defined in the same translation unit where it is used. I think the only
way to do it is define inline functions in header files, #include them,
and hope for them to be inlined by the compiler.

You may be able to influence that with compiler specific settings that
are not on topic here (so any questions about them should be
discussed elsewhere).

<OT> Example with gcc : see docs at

http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Optimize-Options.html#Optimize-Options

In particular, the -finline-functions argument and others </OT>

-David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

inline functions 50
inline 5
Inline functions and warning 7
inline functions 2
how to inline member function is a separate file 6
Inline Functions? 3
Inline functions and linkage 5
inline vs. function pointers 36

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top