470,835 Members | 1,872 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,835 developers. It's quick & easy.

inline functions not inlined

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 7 '06 #1
9 2385
Bi*************@gmail.com 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) ks***@mib.org <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.
Mar 7 '06 #2
Keith Thompson wrote:
Bi*************@gmail.com 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.
Mar 7 '06 #3
Bi*************@gmail.com 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.
Mar 7 '06 #4
Ian Collins <ia******@hotmail.com> writes:
Keith Thompson wrote:
Bi*************@gmail.com 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) ks***@mib.org <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.
Mar 7 '06 #5
Keith Thompson wrote:
Ian Collins <ia******@hotmail.com> 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.
Mar 7 '06 #6

"Micah Cowan" <mi***@cowan.name> wrote in message
news:87************@mcowan.barracudanetworks.com.. .
Bi*************@gmail.com 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
Mar 7 '06 #7
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 '06 #8

<Bi*************@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr 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
Mar 7 '06 #9
Bi*************@gmail.com 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

Mar 7 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by A | last post: by
47 posts views Thread by Richard Hayden | last post: by
5 posts views Thread by Ondrej Spanel | last post: by
33 posts views Thread by Robert Seacord | last post: by
12 posts views Thread by sam_cit | last post: by
2 posts views Thread by aaragon | last post: by
4 posts views Thread by raashid bhatt | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.