22.2 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® XC16 Assembler, Linker and Utilities User’s Guide (DS50002106). However, you can easily replace a library module that is linked into your program without having to do this.

If you add a source file to your project which contains the definition for a routine with the same name as a library routine, then the library routine will be replaced by your routine.

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 libraries 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 written 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 the Variable Attributes section.