J
James Kuyper
Andreas said:Yes, but IMHO this would not solve my problem, because then I may only
use the inline function in *one* TU.
(Unless I'm thinking the wrong way here)
You probably are. You can use the inline function in every TU where you
#include the appropriate header.
....
Yeah. That might be it.
So this should look like this:
(TU#1)
inline void blah(int x) {/* code */ }
*AND*
(TU#2)
extern inline void blah(int x);
That's not permitted; if you declare an inline function, there must
ALWAYS be a definition of it in the same translation unit; should the
implementation choose to inline the function, that's the definition it
will inline. That's why an inline function to be used in multiple TUs
should be declared AND defined in a header file that is #included
wherever needed. That's equally true whether it has internal or external
linkage.
*AND*
(TU#3)
extern inline void blah(int x) {/* code */ }
aw'ite?
The tricky bit is having the /* code */ TWICE.
You're correct about that being a problem. Here's how to avoid it. As I
understand it (I have never actually needed to do this), the right
approach to creating an inline function with external linkage is:
blah.h:
inline void blah(int x) {/* code */ }
blah.c:
#include "blah.h"
extern void blah(int x);
/* Note that the "extern" on this declaration causes the
* definition in "blah.h" to become an external definition
* rather than an inline definition. */
foo.c:
#include "blah.h"
bar.c
#include "blah.h"