You copied the Doc URL to your clipboard.

Tailoring the C library to your target hardware

You can provide your own implementations of C library functions to override the default behavior.

By default, the C library uses semihosting to provide device driver level functionality, enabling a host computer to act as an input and an output device. This functionality is useful because development hardware often does not have all the input and output facilities of the final system.

You can provide your own implementation of target-dependent C library functions to use target hardware. Your implementations are automatically linked in to your image instead of the C library implementations. The following figure shows this process, which is known as retargeting the C library.

Figure 8-5 Retargeting the C library


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

Example implementation of fputc()

In this example, fputc() redirects the input character parameter to a serial output function sendchar(). fputc() assumes that sendchar() is 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.

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

In a standalone application, you are unlikely to support semihosting operations. Therefore, you must remove all calls to target-dependent C library functions or re-implement them with non-semihosting functions.