views:

144

answers:

1

As I know, C inline function body should be defined in .h file because it causes an error 'function-name used but never defined" if body defined in .c file.

Is this the regular way? Or how to define inline function body in .c file?

+5  A: 

Each .c file is compiled independently into .o output. If you define the inline function in a .c file, other source files cannot see such function, so that cannot be inlined.

Therefore the inline function should be in the .h file to allow the code to be shared.

KennyTM
Thanks! You're saving me a lot :)
Eonil
Have you profiled your code to see if that function **needs** to be inlined, or is this a case of http://goo.gl/bquz ?
JUST MY correct OPINION
@ttmrichter: (off topic) This is not twitter, there's no need to Premature Shorten the URL.
KennyTM
@Eonil: Note that if an inline function is only ever used within one .c file then it can be defined as `static inline` within that .c file rather than in a .h file. Otherwise, as already stated, it needs to me made visible in a .h file.
Paul R
@KennyTM: I'm sure that is a good witticism, but I'm not positive what you mean. (I don't do the twitting thing, so if it relies on knowing specifics of twitting it will definitely fly over my head.)
JUST MY correct OPINION
@ttmrichter: What I mean is you can enter 600 characters here so you can just post the whole URL http://c2.com/cgi/wiki?PrematureOptimization.
KennyTM
@KennyTM: Ah. I see what you mean. I do the whole URL-squishing thing by default these days because so many URLs are hundreds of characters long. It's just motor memory. :)
JUST MY correct OPINION