Keeping Unused Functions

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:

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.