Microchip Technology SW006021-2 User Manual
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 198
2012 Microchip Technology Inc.
5.11
LIBRARY ROUTINES
5.11.0.1
USING LIBRARY ROUTINES
Library functions (and any associated variables) will be automatically linked into a pro-
gram once they have been referenced in your source code. The use of a function from
one library file will not include any other functions from that library. Only used library
functions will be linked into the program output and consume memory.
gram once they have been referenced in your source code. The use of a function from
one library file will not include any other functions from that library. Only used library
functions will be linked into the program output and consume memory.
Your program will require declarations for any functions or symbols used from libraries.
These are contained in the standard C header (.h) files. Header files are not library
files and the two files types should not be confused. Library files contain precompiled
code, typically functions and variable definitions; the header files provide declarations
(as opposed to definitions) for functions, variables and types in the library files, as well
as other preprocessor macros.
These are contained in the standard C header (.h) files. Header files are not library
files and the two files types should not be confused. Library files contain precompiled
code, typically functions and variable definitions; the header files provide declarations
(as opposed to definitions) for functions, variables and types in the library files, as well
as other preprocessor macros.
In the following example, the definition for sqrt is not contained in source code, so the
compiler searches the libraries to find a definition there. Once found, it links in the
function for sqrt into your program.
compiler searches the libraries to find a definition there. Once found, it links in the
function for sqrt into your program.
#include <math.h> // declare function prototype for sqrt
void main(void)
{
double i;
// sqrt referenced; sqrt will be linked in from library file
i = sqrt(23.5);
}
5.11.1
The printf Routine
The code associated with the printf function is not precompiled into the library files.
The printf() function is generated from a special C template file that is customized
after analysis of the user’s C code. See Section “PRINTF, VPRINTF” for more infor-
mation on using the printf library function.
The printf() function is generated from a special C template file that is customized
after analysis of the user’s C code. See Section “PRINTF, VPRINTF” for more infor-
mation on using the printf library function.
The template file is found in the lib directory of the compiler distribution and is called
doprnt.c
doprnt.c
. It contains a minimal implementation of the printf() function, but with
the more advanced features included as conditional code which can be utilized via
preprocessor macros that are defined when it (along with your code) is compiled.
preprocessor macros that are defined when it (along with your code) is compiled.
The parser and code generator analyze the C source code, searching for calls to the
printf
printf
function. For all calls, the placeholders that were specified in the printf()
format strings are collated to produce a list of the desired functionality of the final func-
tion. The doprnt.c file is then preprocessed with the those macros specified by the
preliminary analysis, thus creating a custom printf() function for the project being
compiled. After parsing, the p-code output derived from doprnt.c is then combined
with the remainder of the C program in the final code generation step.
tion. The doprnt.c file is then preprocessed with the those macros specified by the
preliminary analysis, thus creating a custom printf() function for the project being
compiled. After parsing, the p-code output derived from doprnt.c is then combined
with the remainder of the C program in the final code generation step.
For example, if a program contains one call to printf(), which looks like:
printf(”input is: %d”);
The compiler will note that only the %d placeholder is used and the doprnt.c module
that is linked into the program will only contain code that handles printing of decimal
integers.
that is linked into the program will only contain code that handles printing of decimal
integers.
Consider now that the code is changed and another call to printf() is added. The
new call looks like:
new call looks like:
printf(”output is %6d”);
Now the compiler will detect that additional code to handle printing decimal integers to
a specific width must be enabled as well.
a specific width must be enabled as well.