Say I have a class like this
// DummyObject.h
@interface DummyObject : NSObject
- (instancetype)initWithFoo:(NSString *)foo
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end
and
// DummyObject.m
inline void
foobar() {}
static inline void
static_foobar() {}
@implementation DummyObject
- (instancetype)initWithFoo:(NSString *)foo
{
if (self = [super init])
{
// Undefined symbols for architecture x86_64:
// "_foobar", referenced from:
// -[DummyObject initWithFoo:] in DummyObject.o
foobar();
static_foobar(); // Works as expected
}
return self;
}
@end
My understanding about inline is that it simply suggest the compiler to embed the function body into the calling code instead of executing an actual call. And static refers to the scope, which means I can only use this very function inside the file that defines it.
Update 1:
According to trungduc's comment, "inline foobar" works after change DummyObject.m to DummyObject.mm.
So my current conclusion is
- If DummyObject is compiled as Objective-C(.m), then "inline foobar" won't work
- If DummyObject is compiled as Objective-C++(.mm), then "inline foobar" works fine
- If I delete the "inline" keyword, then "foobar" works fine in both Objective-C and Objective-C++
- "static inline static_foobar" always works
This is quite confusing, I've checked the Clang documentation with no luck. This has been in my head for about 2 days, any suggestions will help...