Search code examples
objective-cxcodestatic-linking

Duplicate extern constant when linking static library


I wrote libraryA.

//classA.h
extern NSString *const ConstantString;

//classA.m
NSString *const ConstantString = @"aaa";

I wrote libraryB

//classB.m
#import <libraryA/classA.h> 

Also libraryA.a added to of libraryB -> build phase -> link binary with libraries.

Then in my main project, I need to import classA and classB together.So I added libraryA.a and libraryB.a both to main project -> build phase -> link binary with libraries.

There comes the problem when linking.It says duplicate symbol of ConstantString.

So, how to fix it?


Solution

  • Thanks for Jason's comment at very first. I think I found the right solution after discussion with my colleagues. libraryB should not link against libraryA after compilation. Linking should be done after compiling the main project. I removed libraryA from libraryB -> build phase -> link binary with libraries. Error never shows again.