0

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

  1. If DummyObject is compiled as Objective-C(.m), then "inline foobar" won't work
  2. If DummyObject is compiled as Objective-C++(.mm), then "inline foobar" works fine
  3. If I delete the "inline" keyword, then "foobar" works fine in both Objective-C and Objective-C++
  4. "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...

NSFish
  • 354
  • 3
  • 10
  • 1
    Is this Objective-C? It doesn't look like C. – dbush Oct 10 '18 at 15:23
  • @dbush Yes it is Objective-C, I'm using some structs and C functions inside Objective-C method. – NSFish Oct 11 '18 at 02:41
  • @NSFish Objective-C doesn't have keyword `inline` as I know. It should be `NS_INLINE` or rename `DummyObject.m` to `DummyObject.mm` to make this file become Objective-C++ file and it will work – trungduc Oct 11 '18 at 09:06
  • @trungduc Thanks for your reply. DummyObject.mm did work. But I still don't know why Objective-C method can call normal C functions without problems, yet inline functions won't work... – NSFish Nov 06 '18 at 08:10
  • @NSFish Because Objective-C is a thin layer atop C, and is a "strict superset" of C, meaning that it is possible to compile any C program with an Objective-C compiler, and to freely include C language code within an Objective-C. You can take a look at Wikipedia for Objective-C https://en.wikipedia.org/wiki/Objective-C – trungduc Nov 06 '18 at 08:23
  • @trungduc I'm aware of what you said, which doesn't answer my question. Since Objective-C is a thin layer on top of C, the compiler(in this case, Clang), is supposed to recognize any C functions. Yet the example I wrote shows that's not the case, inline C functions are left alone... – NSFish Nov 07 '18 at 13:44
  • @NSFish You should understand what is difference of between `inline` in C and C++. Take a look at this post https://stackoverflow.com/questions/31108159/what-is-the-use-of-the-inline-keyword-in-c – trungduc Nov 07 '18 at 13:54
  • @trungduc Thanks again! This link saves my life . Now I understand. – NSFish Nov 21 '18 at 03:46

0 Answers0