27

I want to write an inline function, but I get an error. How can I fix it?

Error information:

Undefined symbols for architecture i386:
  "_XYInRect", referenced from:
      -[BeginAnimation ccTouchesEnded:withEvent:] in BeginAnimation.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Code:

CGPoint location = CGPointMake(60, 350);

if (XYInRect(location, 53, 338, 263, 369)) {

}

inline BOOL XYInRect(CGPoint location, float MixX, float MixY, float MaxX ,float MaxY){
    if (location.x >MixX && location.y >MixY && location.x <MaxX && location.y <MaxY) {
        return YES;
    } else {
        return NO;

    }
}
jtbandes
  • 115,675
  • 35
  • 233
  • 266
user1297301
  • 887
  • 1
  • 9
  • 12
  • 4
    Not an answer, but can't you just use CGRectContainsPoint instead of rolling your own? – jrturton Apr 20 '12 at 08:57
  • Compiling with the -O2 optimization switch worked for me, as suggested in http://StackOverflow.com/questions/18939482/clang-linker-fails-if-at-least-o2-wasnt-used and another question I can no longer find. – Devon Dec 29 '17 at 02:06

2 Answers2

42

Clang defaults to C99 and not GNU sematics, which means a raw inline is different from both static inline and extern inline.

In particular, a raw inline means that the function still has external linkage, but the inline definition does not provide the external one (you'd need extern inline for that).

What this means is that you need an additional extern definition in a different translation unit or linking will fail. However, you're probably looking for static inline.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Christoph
  • 164,997
  • 36
  • 182
  • 240
  • Note that by the time XCode 8.1 was released (2016-10-27), the default language for (Apple's release of) `clang` was no longer C99 but C11. GCC 5.0 and later also default to C11 (but previously defaulted to C90). This is only a minor detail; it doesn't affect the rest of the answer. – Jonathan Leffler Oct 28 '16 at 04:04
  • This issue occurs even in a single translation unit though, where it is not referenced from anywhere else. – user129393192 Aug 04 '23 at 13:43
  • For example, `inline int foo () {} int main() { foo(); }` – user129393192 Aug 04 '23 at 13:43
0

I think XYInRect() needs to be known prior to being used. Move the function definition to some place in the file before you call it.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180