2.3.1. Retargeting the C library

You can provide your own implementation of C Library functions that make use of target hardware, and that are automatically linked in to your image in favor of the C library implementations. This process, known as retargeting the C library, is shown in Figure 2.6.

Figure 2.6. Retargeting the C library

For example, you might have a peripheral I/O device such as a UART, and you might want to override the library implementation of fputc(), that writes to the debugger console, with one that outputs to the UART. Because this implementation of fputc() is linked in to the final image, the entire printf() family of functions prints out to the UART.

Example 2.1 shows an example implementation of fputc(). The example redirects the input character parameter of fputc() to a serial output function sendchar() that is assumed to be implemented in a separate source file. In this way, fputc() acts as an abstraction layer between target dependent output and the C library standard output functions.

Example 2.1. Implementation of fputc()

extern void sendchar(char *ch);

int fputc(int ch, FILE *f)
{   /* e.g. write a character to an UART */
    char tempch = ch;
    sendchar(&tempch);
    return ch;
}

Copyright © 2002-2006 ARM Limited. All rights reserved.ARM DUI 0203G
Non-Confidential