3.5 Library Functions
The MPLAB XC8 compiler does not provide the AVR-libc standard C library for AVR-GCC. Some functions provided in AVR-libc are not implemented in the Microchip library, and projects using these functions must provide alternate definitions or be recoded.
Description
MPLAB XC8 ships with a Microchip Universal Standard Library, which is shared between all MPLAB XC compilers. This library contains only functions that form part of the C standard. Several non-standard functions are provided by AVR-libc and any project utilizing these functions will fail to build when migrated to MPLAB XC8. Some non-standard functions are only partially implemented by the Microchip Universal Standard Library and although their use will not trigger build errors, their execution will not produce the expected results.
Functions | Header |
---|---|
dtostre() ,
dtostrf() | <stdlib.h> |
getenv() | <stdlib.h> |
itoa() | <stdlib.h> |
random() ,
srandom() | <stdlib.h> |
system() | <stdlib.h> |
utoa() , ultoa() | <stdlib.h> |
strcasecmp() , strlwr() ,
strupr() , strncasecmp() ,
strrev() | <string.h> |
ffs() , ffsl() ,
ffsll() | <string.h> |
square() | <math.h> |
mk_gmtime() , sun_rise() ,
sun_set() ,
solar_noon() | <time.h> |
gmtime_r() ,
localtime_r() | <time.h> |
asctime_r() , ctime_r() ,
isotime() ,
isotime_r() | <time.h> |
set_dst() , set_zone() ,
set_system_time() ,
system_tick() ,
set_position() | <time.h> |
is_leap_year() ,
month_length() , week_of_year() ,
month_of_year() ,
fatfs_time() , equation_of_time() ,
daylight_seconds() ,
moon_phase() | <time.h> |
iso_week_date() ,
iso_week_date_r() | <time.h> |
solar_declination() ,
gm_sideral() ,
lm_sideral() | <time.h> |
Migration
To migrate projects that have used any non-standard AVR-libc functions, the easiest option would be to download the AVR-libc to obtain the source files for these functions and include these into your project.
This Online Microchip Help web page documents the AVR-libc. It also contains a link to the AVR Libc web page where you can find a link to the download area. Download the required AVR-libc archive and unzip it to a local directory. You can include source files into your projects directly from this local directory or copy the required source files into your project directory and then add them to your project from that location. Note that some functions are written in assembly code.
The declarations for any non-standard functions can be taken from the AVR-libc
headers. You can provide the MPLAB XC8 preprocessor with the path to this header in
the unzipped local directory, or alternatively the path to copies of this header in
your project directory. The -I
compiler option allows the path to
be specified, and this can be specified in the “Include directories” field, found in
the “Preprocessing and messages” options within the “XC8 Compiler” category of the
Project Properties dialog. AVR-libc headers should be scanned first before the
standard headers; however, this will be the case if you add the search path as
described above.
On rare occasions, there might be conflicting information in code or headers provided by AVR-libc and those provided by the libraries shipped with MPLAB XC8. If the compiler sees a declaration from an AVR-libc header but links in the corresponding definition from the compiler-shipped library (or vice versa), the project might fail to build or execute correctly if there is a mismatch in that information. Where there is such a conflict, you could selectively copy those declarations required from AVR-libc headers and create your own header file from these.