iOS Build a Diary App Using Core Data Inserting and Saving Data Inserting a Managed Object

Cristian Malita
Cristian Malita
4,103 Points

Linker error?

I'm getting the following error when I'm trying to build the project:

duplicate symbol _DiaryEntryMood in: /Users/cristianmalita/Library/Developer/Xcode/DerivedData/Diary-aosjsfqbxxmkfgcbspiawmuevdtj/Build/Intermediates/Diary.build/Debug-iphonesimulator/Diary.build/Objects-normal/x86_64/DiaryEntry.o /Users/cristianmalita/Library/Developer/Xcode/DerivedData/Diary-aosjsfqbxxmkfgcbspiawmuevdtj/Build/Intermediates/Diary.build/Debug-iphonesimulator/Diary.build/Objects-normal/x86_64/NewEntryViewController.o ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems that if I remove the NS_ENUM from the DiaryEntry.h file, it works. I've pretty much tried everything, including starting a new project and writing it all over again from scratch. If I download the project files, they work fine, but I want to know what I'm doing wrong.

3 Answers

Cristian Malita
Cristian Malita
4,103 Points

Ok, I have managed to solve this, I'm answering my own question in case someone else encounters the same problem. The problem seems to be that the "No common blocks" in the "Apple LLVM 6.1 - Code Generation" section in the Build settings pane is set to Yes, in the latest version of Xcode.

There are two ways I could solve this. One was to navigate to Diary in project navigator, select Build Settings, scroll down to Apple LLVM 6.1 Code Generation and change the No common blocks option to No. The second (and probably the better) way, was to just use the extern keyword in front of NS_ENUM in DiaryEntry.h (or THDiaryEntry.h, depending on how you have it set up).

Eduardo Mejía
Eduardo Mejía
36,816 Points

You are the man! Thanks Cristian. I had the same problem. I wrote the extern keyword in front of NS_ENUM and it worked. But why do we have to write the extern keyword in the latest version of Xcode?

Cristian Malita
Cristian Malita
4,103 Points

According to Apple, this is what the No common blocks option does:

In C, allocate even uninitialized global variables in the data section of the object file, rather than generating them as common blocks. This has the effect that if the same variable is declared (without extern ) in two different compilations, you will get an error when you link them. The only reason this might be useful is if you wish to verify that the program will work on other systems which always work this way.

Basically, the No common blocks option was disabled in previous versions of Xcode, so that's why Ash didn't get any error in the video. Having the option enabled or disabled won't make any difference for this project, but if you have it enabled (which it is by default in the latest version of Xcode) you have to use the extern keyword for it to work. I think Ash should update the teacher notes and perhaps provide a better explanation.

Thank you for posting the solution Cristian.

Florian Thompson
PRO
Florian Thompson
Pro Student 18,609 Points

You sir deserve a hell lot of cookies. Thank you very much!