Keeping Unused Functions
One feature of our 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 to 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:
2
3
4
{
…
}
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 in-line assembly code. If there is some other reason why you want the function preserved, reference the function's symbol from the in-line assembly, ensuring you use the assembly-form of the symbol with a leading underscore character. The following code, defining the function's symbol using the GLOBAL directive, is sufficient and does not consume any memory:
2
3
4
5
6
7
asm("GLOBAL _myFunc");
void myFunc(void)
{
…
}