Xcode 中大量報(bào)重復(fù)引用錯(cuò)誤的一種可能

? 在重構(gòu)程序修改了許多地方代碼后運(yùn)行,xcode 報(bào)了1k+的重復(fù)引用錯(cuò)誤容达,經(jīng)過(guò)排查發(fā)現(xiàn)是常量定義出現(xiàn)的,在此引用stackoverflow上的一段回答解釋花盐。

原回答link http://stackoverflow.com/questions/16144666/apple-mach-o-linker-error-with-duplicate-symbols-even-after-linking-the-m-class

? 事實(shí)上是在一個(gè)全局參數(shù)頭文件.h中使用const來(lái)定義常量導(dǎo)致出錯(cuò) ?改用define后就可以。

? 但是只是用define無(wú)法對(duì)指針進(jìn)行比較操作柒昏,更好的方法是使用const來(lái)定義熙揍。

? extern c關(guān)鍵字,表示這個(gè)變量已經(jīng)聲明,只是引用是尖。const關(guān)鍵字表示變量是常量泥耀,不可修改。

#import

extern NSString * const kInitURL;

@interface Constants : NSObject {

}

@end

? .m文件賦值

#import "Constants.h"

NSString * const kInitURL = @"http://marshal.easymorse.com";

@implementation Constants

@end


? 之后引入Constans.h就可以使用常量了痰催。



參考:

? This is really really basic beginner stuff that suggests to me you don't understand the difference between C-family language's "header files" and "implementation files".Did you search your project and not notice that you are declaring variables in header files but without extern? You even had the name of the global variable in your linker error. Yes, global variables are emitted as symbols into object files (.o) and if you include a header into three files and all three files then declare the same global variable, you get this error. It was for just this issue that the mighty and wonderful extern keyword was invented.In the context of header files you should not declare actual variables, only extern references to them. This is something you should learn on your first day of C, C++ or Objective C programming. You can however put code into header files, but then, every file you include that header into will generate a duplicate set of symbols, leading to link errors, and further accelerating the pace of global warming, and causing the melting of icebergs. (Just kidding about that last part.)Here is your mistake, this is a header file but you are treating it like it was an implementation file and declaring not external-variable-declarations but actual-emit-the-global-variable-into-the-object-file declarations:

#importUIImage *SavedImage;

UIImage *SavedImage1;

NSString * vvvalue;

//UILabel * l2;

NSString * lll;

@interface NSObject (ShareClass)

@end

? You tell me now. Where should the keyword extern appear in the code sample above? Where does the real "emit this variable into object file" declaration of a variable go? In a header file? NO. In the implementation file (.m or .c or .cpp as the case may be).

? Secondly, after you add the extern keyword, you now have a second problem. You probably need to add the real declaration of that global variable in your corresponding .m file. Yes, that's right you have to "declare" every global twice in a C-family language program. once as extern in header files if you really need all the modules in your system to be able to see that global variable, and once without the extern keyword, in only one of your .m files.

? Of course, you do know that such "global variable mess" is to be avoided, and is terrible programming practice right? You should be thinking about keeping global variables out of your code, and have variables that limit their scope to a single implementation module wherever possible.

? In .h file:

extern UIImage * myImage;

? In .m file:?

UIImage *myImage;

? Go read your book on what the extern keyword means please. Also, I suspect that you might have meant to do this:

@interface NSObject (ShareClass)

UIImage * InstanceVariable;

@end

? The above is an instance variable (belongs to each instance of the class). It is common if you need to group some stuff and put it inside an object not to make it a global variable.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末逸吵,一起剝皮案震驚了整個(gè)濱河市扫皱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌啸罢,老刑警劉巖编检,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件允懂,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蕾总,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門递雀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蚀浆,“玉大人,你說(shuō)我怎么就攤上這事市俊。” “怎么了摆昧?”我有些...
    開(kāi)封第一講書人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)伺帘。 經(jīng)常有香客問(wèn)我,道長(zhǎng)伪嫁,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任驹吮,我火速辦了婚禮晶伦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘婚陪。我一直安慰自己,他們只是感情好泌参,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布沽一。 她就那樣靜靜地躺著,像睡著了一般铣缠。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蝗蛙,一...
    開(kāi)封第一講書人閱讀 52,736評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音哮内,去河邊找鬼壮韭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛泰涂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播逼蒙,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼僵井!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起批什,我...
    開(kāi)封第一講書人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎乳规,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體暮的,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡淌实,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年拆祈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片放坏。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖犁珠,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情互亮,我是刑警寧澤余素,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布桨吊,位于F島的核電站,受9級(jí)特大地震影響视乐,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜佑淀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧逢倍,春花似錦、人聲如沸较雕。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)宛蚓。三九已至,卻和暖如春凄吏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背痕钢。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工序六, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人例诀。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像繁涂,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子扔罪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容