As described in “Loading,” in some situations your object code may successfully load, using weak linking, but certain function calls may be undefined because they are not available in the current system. To run successfully, your code must avoid calling those functions in system versions that do not support them. It can do this either by checking the system version at run time and globally taking a different code path based on the version, or by checking each function pointer for a null value before calling it.
For example, suppose you build your application to use features in Mac OS X version 10.3 (Panther) and to deploy back to Mac OS X version 10.2. (Jaguar). To use the HIAboutBox function, first available in Panther, you could use code like the following:
Listing 2-1 A function that checks for a null function pointer
void MyAboutBox(void) |
{ |
if(HIAboutBox != NULL) |
{ |
HIAboutBox(NULL); |
} |
else |
{ |
// Lots of code to display an about box with earlier technology. |
} |
} |
Important: When checking for the existence of a symbol, you must explicitly compare it to NULL or nil in your code. You cannot use the negation operator ( ! ) to negate the address of the symbol.
When your code runs in Panther, it calls HIAboutBox to display the minimal default standard About box. When it runs in Jaguar, it displays an About box based on the code you wrote for Jaguar.
If you build this code with different settings, you should see the following results:
If you select an SDK setting of Mac OS X 10.2.8:
The build fails because HIAboutBox is not defined in those system versions.
With an SDK setting of Mac OS X 10.3.9, if you set the deployment OS to:
10.3: The software should run only in Panther and fail to launch in Jaguar or Puma (Mac OS X version 10.1).
10.2: The software should run in Panther and Jaguar but fail to launch in Puma.
10.1: The software should run in Panther but fail to launch in Puma because weak linking is not supported in Puma.
Note: As described in “Limitations,” software built for Panther with a deployment OS of 10.1 will not launch in Jaguar.
Last updated: 2006-11-07