Re: Getting link errors weak-linking against a framework fn
Re: Getting link errors weak-linking against a framework fn
- Subject: Re: Getting link errors weak-linking against a framework fn
- From: Ken Thomases <email@hidden>
- Date: Wed, 9 Jun 2010 20:39:47 -0500
On Jun 9, 2010, at 8:22 PM, Chris Suter wrote:
> On Thu, Jun 10, 2010 at 2:04 AM, Jens Alfke <email@hidden> wrote:
>> I’m having trouble weak-linking against a function in a system framework. It was added in 10.6 but I need my code to run on 10.5, so I check whether the function exists before calling it. But even though I weak-import the function, I get a link error.
>>
>> The function’s not declared in a header*, so I added my own declaration:
>> extern OSStatus SecKeyCreateWithCSSMKey(const CSSM_KEY *key, SecKeyRef* keyRef) WEAK_IMPORT_ATTRIBUTE;
>>
>> I call it as follows:
>> if (SecKeyCreateWithCSSMKey != NULL)
>> SecKeyCreateWithCSSMKey(cssmKey,&keyRef);
>>
>> This works when I build against the 10.6 SDK, but building against 10.5 I get a link error:
>>
>> Undefined symbols:
>> "_SecKeyCreateWithCSSMKey", referenced from:
>> -[MYSymmetricKey _initWithCSSMKey:] in MYSymmetricKey.o
>> _SecKeyCreateWithCSSMKey$non_lazy_ptr in MYSymmetricKey.o
>> (maybe you meant: _SecKeyCreateWithCSSMKey$non_lazy_ptr)
>> ld: symbol(s) not found
>>
>> So am I doing something wrong in my declaration, or is it simply impossible to weak-link with a function that’s newer than the SDK I build against?
>
> I'm not an expert on this but it's my understanding that the linker
> needs to know which library the function is in (it uses two-level
> namespaces). This is to save time; it narrows the search that dyld
> needs to make.
In other words, a weak import doesn't mean it can be absent at link time, only that it can be absent at load time.
If you're building against the 10.5 SDK, you can conditionally compile that section of the code (as well as having the run-time check for the function).
Or, you can use dynamic loading (dlopen, dlsym) to get the function pointer if it's available.
-Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden