2.6 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 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 make use of target hardware. These are automatically linked in to your image in favor of the C library implementations. The following figure shows this process, known as retargeting the C library.

Figure 2-5 Retargeting the C library
To view this graphic, your browser must support the SVG format. Either install a browser with native support, or install an appropriate plugin such as Adobe SVG Viewer.


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

Example implementation of fputc()

In this example implementation of fputc(), the function 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.

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.

Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.