472,984 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 software developers and data experts.

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 2570
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg....
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
5
by: Ondrej Spanel | last post by:
I though that inline functions should be always visible only in the compilation unit where they are defined - meaning if compiler cannot inline them, they should be handled as if declared static....
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
12
by: sam_cit | last post by:
Hi Everyone, I have few questions on inline functions, when i declare a function as inline, is it for sure that the compiler would replace the function call with the actual body of the function?...
2
by: aaragon | last post by:
Hi everyone, I would like to create a very simple function: // header file inline void point_map() { PointMap pointMap = get(vertex_point_t(), g); } // main.cpp
4
by: raashid bhatt | last post by:
hi by the definition of inline functions "In computer science, an inline function is a programming language construct used to suggest to a compiler that a particular function be subjected to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.