2012-04-20 64 views
23

我想編寫一個內聯函數,但出現錯誤。我該如何解決它?內聯函數「未定義符號」錯誤

錯誤信息:

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) 

代碼:

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; 

    } 
} 
+4

不是一個答案,但你不能只用CGRectContainsPoint而不是滾動您自己的? – jrturton 2012-04-20 08:57:01

+0

使用-O2優化開關進行編譯適用於我,如http://StackOverflow.com/questions/18939482/clang-linker-fails-if-least-o2-wasnt-used所示,另一個問題是我無法更長的發現。 – devon 2017-12-29 02:06:14

回答

34

Clang defaults to C99而不是GNU sematics,這意味着原始inline不同於static inlineextern inline兩者。

特別是,原始inline意味着該功能還有外部鏈接,但內聯定義不提供外接一個(你需要extern inline爲)。

這意味着您需要在另一個翻譯單元中額外定義extern,否則鏈接將失敗。但是,您可能正在尋找static inline

+0

這是正確的。好極了!並且,謝謝。 – Daniel 2012-06-05 18:58:12

+0

請注意,在XCode 8.1發佈(2016-10-27)之前,(Apple發佈的)'clang'的默認語言不再是C99,而是C11。 GCC 5.0及更高版本也默認爲C11(但以前默認爲C90)。這只是一個小細節;它不會影響答案的其餘部分。 – 2016-10-28 04:04:28

0

我認爲XYInRect()需要在使用之前是已知的。在您調用函數定義之前,將函數定義移至文件中的某個位置。

+0

這將是一個彙編,而不是一個鏈接錯誤 – Christoph 2012-04-20 12:17:44

0

也建議使用CGRectContainsPoint