inline functions not inlined

Discussion in 'C Programming' started by Bilgehan.Balban@gmail.com, Mar 6, 2006.

  1. Guest

    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
    , Mar 6, 2006
    #1
    1. Advertising

  2. 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) <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.
    Keith Thompson, Mar 7, 2006
    #2
    1. Advertising

  3. Ian Collins Guest

    Keith Thompson wrote:
    > 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.
    Ian Collins, Mar 7, 2006
    #3
  4. Micah Cowan Guest

    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.
    Micah Cowan, Mar 7, 2006
    #4
  5. Ian Collins <> writes:
    > Keith Thompson wrote:
    >> 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) <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.
    Keith Thompson, Mar 7, 2006
    #5
  6. Ian Collins Guest

    Keith Thompson wrote:
    > Ian Collins <> 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.
    Ian Collins, Mar 7, 2006
    #6
  7. "Micah Cowan" <> wrote in message
    news:...
    > 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
    Rod Pemberton, Mar 7, 2006
    #7
  8. Guest

    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
    , Mar 7, 2006
    #8
  9. Robin Haigh Guest

    <> wrote in message
    news:...
    > 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
    Robin Haigh, Mar 7, 2006
    #9
  10. 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.1.0/gcc/Optimize-Options.html#Optimize-Options

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

    -David
    David Resnick, Mar 7, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    3
    Views:
    288
  2. Replies:
    3
    Views:
    451
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    1,837
    Kevin Bracey
    Nov 21, 2003
  4. newbie
    Replies:
    2
    Views:
    380
    Dave Rahardja
    Apr 28, 2007
  5. Michal
    Replies:
    4
    Views:
    341
    Jorgen Grahn
    Nov 22, 2008
Loading...

Share This Page