Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > inline functions not inlined

Reply
Thread Tools

inline functions not inlined

 
 
Bilgehan.Balban@gmail.com
Guest
Posts: n/a
 
      03-06-2006
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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2006
Keith Thompson wrote:
> (E-Mail Removed) writes:
>
>>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).
>

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

--
Ian Collins.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-07-2006
(E-Mail Removed) writes:

> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-07-2006
Ian Collins <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> (E-Mail Removed) writes:
>>
>>>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).
>>

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

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-07-2006
Keith Thompson wrote:
> Ian Collins <(E-Mail Removed)> writes:
>
>>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.)
>

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.

--
Ian Collins.
 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      03-07-2006

"Micah Cowan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> (E-Mail Removed) writes:
>
> > 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.


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


 
Reply With Quote
 
Bilgehan.Balban@gmail.com
Guest
Posts: n/a
 
      03-07-2006
Keith Thompson wrote:
> 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

 
Reply With Quote
 
Robin Haigh
Guest
Posts: n/a
 
      03-07-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.

--
RSH


 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      03-07-2006
(E-Mail Removed) wrote:
>
> 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....timize-Options

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

-David

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux g++: how to prevent ALL functions being inlined Michal C++ 4 11-22-2008 11:52 PM
why template functions in boost lib are all inlined? newbie C++ 2 04-27-2007 11:21 PM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
Member Functions in class scope are auto inlined? Andy C++ 3 12-18-2003 05:27 PM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM



Advertisments