One of the features of the MPLAB® XC compilers is that they can remove C functions that have not been called. This allows you to have a file full of useful functions, but only those that are used are linked into your project and consume memory. However, occasionally you might want a function to be linked into a program even though it has not been called from C code. Such might be the case if the function was called from assembly code or some source external to the C program in which the function is defined.
The MPLAB XC16/32 compilers will remove any unused static function, or any other unused function if the whole-program and link-time optimizations option is set. To prevent a function from being removed, specify the function using the used attribute, for example:
The MPLAB XC8 compiler will remove all unused functions, regardless of how they are defined. If you intend to call a function only from assembly code, there is nothing you need to do. The compiler will detect that the function is accessed in the assembly code and automatically prevent it from being removed. This works for access from assembler modules or from in-line assembly code. If there is some other reason why you want the function preserved, reference the function's symbol from in-line assembly, ensuring you use the assembly-form of the symbol with a leading underscore character. The following code, which simply defines the function's symbol using the GLOBAL directive, is sufficient and does not consume any memory: