Re: Inline C functions in Xcode 3
Re: Inline C functions in Xcode 3
- Subject: Re: Inline C functions in Xcode 3
- From: Steve Checkoway <email@hidden>
- Date: Sat, 3 Nov 2007 01:20:56 -0700
On Nov 2, 2007, at 7:08 PM, David Riggle wrote:
My inline C functions that compiled fine under Xcode 2.4 are giving
me errors in Xcode 3. I use C99 mode. I want to declare the
functions in a .h and implement them in a .c. Is that still
possible? If so, which magic incantations of inline, static inline,
or extern inline do I need to use?
Since gcc compiles each object file separately from the others,
declaring a function inline in a header but not making that definition
visible is going to require an out of line copy of the function to be
emitted which is what is being linked against, unless ld is doing
optimization across translation units. A quick test seems to indicate
that ld is not inlining functions across translation units.
For example,
// inline.h
static inline int foo() { return 5; }
inline int bar();
// bar.c
#include "inline.h"
inline int bar() { return 5; }
// main.c
#include "inline.h"
int main() { return foo() != bar(); }
If I compile this without using -std=c99, main makes a function call
to bar, but not foo and the program links.
If I compile it as c99, main.c gives me a warning (actually repeated
for some reason) "inline.h:2: warning: inline function ‘bar’ declared
but never defined" whereas bar.c gives me no warning but doesn't
actually contain any symbols. It fails to link in this case because
there is no symbol _bar.
<http://www.greenend.org.uk/rjk/2003/03/inline.html> gives the various
rules for inline. Note the differences between c99 and gnu C,
especially in the case of all functions defined with inline.
I'm not sure how any of this changed with Xcode 3. Basically, if you
want an inline function used in several translation units, then
declare it static inline [and possibly __attribute__((always_inline))]
and provide the definition in the header file.
--
Steve Checkoway
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden