Microchip Technology SW006022-2N Data Sheet

Page of 338
Linking Programs
 2012 Microchip Technology Inc.
DS52071B-page 209
17.3
REPLACING LIBRARY SYMBOLS
The MPLAB XC16 C Compiler comes with a librarian which allows you to unpack a 
library file and replace modules with your own modified versions. See the MPLAB 
Assembler, Linker and Utilities for PIC24 MCUs and dsPIC DSCs User’s Guide 
(DS51317)
. However, you can easily replace a library module that is linked into your 
program without having to do this.
If you add to your project a source file which contains the definition for a routine with 
the same name as a library routine, then the library routine will be replaced by your rou-
tine.
When trying to resolve a symbol (a function name, or variable name, for example) the 
compiler first scans all the source modules for the definition. Only if it cannot resolve 
the symbol in these files does it then search the library files.
If the symbol is defined in a source file, the compiler will never actually search the librar-
ies for this symbol and no error will result even if the symbol was present in the library 
files. This may not be true if a symbol is defined twice in source files and an error may 
result if there is a conflict in the definitions.
Another method is to use the weak attribute when declaring a symbol. A weak symbol 
may be superseded by a global definition. When weak is applied to a reference to an 
external symbol, the symbol is not required for linking. 
The weak attribute may be applied to functions as well as variables. Code may be writ-
ten such that the function will be used only if it is linked in from some other module. 
Deciding whether or not to use the feature becomes a link-time decision, not a compile 
time decision.
For more information on the weak attribute, see Section 6.11 “Variable Attributes”.
17.4
LINKER-DEFINED SYMBOLS
The 16-bit linker defines several symbols that may be used in your C code develop-
ment. Please see the MPLAB Assembler, Linker and Utilities for PIC24 MCUs and 
dsPIC DSCs User’s Guide (DS51317)
 for more information.
A useful address symbol, _PROGRAM_END, is defined in program memory to mark the 
highest address used by a CODE or PSV section. It should be referenced with the 
address operator (&) in a built-in function call that accepts the address of an object in 
program memory. This symbol can be used by applications as an end point for check-
sum calculations. 
For example:
unsigned int end_page, end_offset;
 _prog_addressT big_addr;
end_page   =  __builtin_tblpage(&_PROGRAM_END);
end_offset = __builtin_tbloffset(&_PROGRAM_END);
 
_init_prog_address(big_addr, _PROGRAM_END);