iOS-Universal-Framework
iOS-Universal-Framework copied to clipboard
Can't see extern declared vars/functions in framework.
In my framework I declare something like:
TestFramework.h
extern char const *test;
extern NSString const *testString;
@interface TestFramework : NSObject
@end
TestFramework.m
#import "TestFramework.h"
char const *test;
NSString const *testString;
@implementation TestFramework
- (id)init {
if ((self = [super init])) {
test = "This is a test string.";
testString = @"This is another test string.";
}
return self;
}
@end
This builds fine, I add the .h file to the public section of the Copy Headers build phase.
I can then use this framework in another project, and the header file includes fine. However linking fails:
Undefined symbols for architecture armv7:
"_testString", referenced from:
-[ViewController viewWillAppear:] in ViewController.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What is the solution to this? A solution is not Don't use extern. :D
** Epiphany... the solution is literally "don't use extern", I thought it would be necessary as the code wants to access a variable that is a constant in another place. Works fine without though. **
Can someone clarify why I shouldn't use extern, in my mind the headers are being used in my Application which is using 'The Framework'. Thus the char * or NSString * is externally defined?
I'm not sure of the reason for your linker error, however I thought I'd mention that I use extern frequently throughout my own frameworks and they work just fine. I do see linker errors like that pop up relatively frequently though, but they tend to be caused when I add new files to a framework (I often end up having to delete, rebuild & re-add the framework in question).
It's interesting that it is _testString being referenced, not just testString which is the global variable being declared. What is the code inside viewWillAppear that references it?
Essentially, given that the above example is simply a test framework and application I knocked up to demonstrate my issue:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[TestFrameWork alloc] init];
self.aLabel.text = (NSString*)testString;
}
Which works. I still cannot quite fathom how it's compiling and working without the extern declared. Is this a symptom of iOS and static linking? The framework is using the 'Real' Static Framework template. E.g. the one that requires other people using it to have modified their copy of xcode in the same way.
At the moment I am sans XCode due to a rather slow update process, but will be trying to work out where my folly lies later on.
In case anyone is having the same issue with "FOUNDATION_EXPORT", which is what you should be using instead of extern, same problem.
The FOUNDATION_EXPORT macro parses down to different types of "extern" calls depending on a few different variables, but same issue. It can't find it until you remove the FOUNDATION_EXPORT. Weird!