Inline, external call and gcc?

Jan 17, 2014 at 9:49am
I have some functions inlined using the inline function prefix. If the function is called from outside the file (so a seperate psp-gcc -O3 ... filename.c filename.o compile command, when only the function is changed), will the other files be updated too? (I'm using the pspsdk toolchain).

Example:

max.c
1
2
3
4
inline byte max(byte a, byte b)
{
	return a>b?a:b;
}


use1.c
1
2
3
4
5
6
7
void use1()
{
if (max(1,2)==0)
{
//Something } 
}
}


use2.c
1
2
3
4
5
6
7
void use1()
{
if (max(2,3)==0)
{
//Something } 
}
}


If I compile this, next change the max function and recompile using make (the compiler only takes the changed max.c->max.o file, next links them together) will use1.c&use2.c be updated with the new max.c function?
Jan 17, 2014 at 10:45am
If you don't change the interface to your function, just the implementation, then use1.c and use2.c don't need to be updated. The linker will re-link use1.o and use2.o with the new version of max.o, so that the new version of the max function will be called.

If you change the interface to the function - e.g. you change the name, or the arguments to it, then you'll presumably change the header file that contains the declaration of the function. This means that every file that includes that header will need to be recompiled. You'll also have to edit use1.c and use2.c yourself to change the function calls. Unless you're using an IDE with some advanced refactoring functionality, those source files won't magically get updated.


Never mind - I missed the part about max being an inline function. I haven't used one of those in years, so I can't answer.
Last edited on Jan 17, 2014 at 10:48am
Jan 17, 2014 at 11:03am
If the function is called from outside the file, will the other files be updated too?
You're not giving us the full picture.

If the other files call max(), inlined in one .c file, how is the declaration of max() available to the other .c files? The prototype must be available before the call is made, so how do you provide that?
Last edited on Jan 17, 2014 at 11:03am
Jan 17, 2014 at 11:19am
For an inline function (even for an inline function with external linkage), the definition must be available to the compiler; place the definition in the header file.

For an inline function with external linkage, it is the responsibility of the implementation to ensure that non-inline usage will refer to the same entity throughout the program. (For instance, the address of an inline function with external linkage would be the same across all translation units.)

If a function is to be inlined only for calls from within the translation unit that it is defined in, do not use the inline keyword. Compilers can, and do perform automatic inlining if the definition is available at compile-time.
Jan 17, 2014 at 4:18pm
I have the inline functions defined as inline within the file and the header. The functions are used by the same .c file, as well as several extern .c files. If I recompile the .c file containing the function itself (with content) with make, will the other files be updated too (by the linker)? Or will only the file containing the function be updated and I have to force recompile the other files using it too, manually (by deleting the .o file containing the calls, next executing make)?
Last edited on Jan 17, 2014 at 4:19pm
Jan 17, 2014 at 4:34pm
> and I have to force recompile the other files using it too
automatically, by putting the corresponding dependencies rules.

> If I recompile the .c file containing the function itself (with content)
¿shouldn't be one definition in each tu that uses the function?
Last edited on Jan 17, 2014 at 4:53pm
Jan 17, 2014 at 4:54pm
> I have the inline functions defined as inline within the file and the header.

Are we talking about C99 inline functions with extrnal linkage? Or is it C++?
The two are completely different beasts.

In C99, this is the way to avoid general insanity re. inline functions with external linkage:

a. Put the definition of the inline function with external linkage in the header file; but do not - repeat do not - use the keyword extern.
For instance, in header foo.h: inline int foo( int a ) { return a+2 ; }

b. In one - and only one - translation unit, include the header file (containing the definition of the function) and then declare the function with the extern keyword. But do not - repeat do not - define it.
For instance, in foo.c:
1
2
3
#include "foo.h"

extern int foo( int a ) ; // do not define 


EDIT: Caveat: AFAIK C99
Last edited on Jan 17, 2014 at 4:57pm
Topic archived. No new replies allowed.