inline trouble with -std=gnu99 (gcc)

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"
 
N

Nick Keighley

Not IMO. To declare is to state _that_ something is, whereas to define
is to state _what_ it is. When Oscar Wilde had nothing to declare except
his genius, he certainly didn't expect to have to explain exactly why
"Earnest" is so hilarious (which was just as well, since, given that it
would have been New York Customs House officers trying to understand the
humour, it would have been completely in vain).

And that's precisely how C has it.


Algol-60 (and I *think* Pascal) used "declare" in the sense that
C uses "define".

I tended to think of define as saying what "shape" it was and
"declaring" actually setting aside the store. And having
said that it will take me another 5 years to sort it out again!
I have "C: one definition rule" written on the wall next to me
for exactly this reason ("oh yes, you can only have one *definition*
of something C")
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top