ARM Technical Support Knowledge Articles

How do I retarget C++ streamed I/O?

Applies to: ARM Developer Suite (ADS), DS-5, RealView Developer Kit (RVDK) for OKI, RealView Developer Kit (RVDK) for ST, RealView Developer Kit for XScale (RVXDK), RealView Development Suite (RVDS)


The C++ libraries depend on the C library for target-specific support. There are no target dependencies in the C++ libraries.

To retarget your C++ streamed I/O, we recommend that you retarget the _sys_xxxx driver level functions. These are the functions used by the C and C++ library and are responsible for target specific I/O. The default implementation uses Semihosting to handle I/O on the host machine.

The ARM Compiler toolchain's C and C++ libraries documentation discusses which functions have to be re-implemented. A summary is shown below:

For an example file (retarget.c), used for redirecting C++ streamed I/O to an UART (connected to e.g. a Terminal Emulator), click here.

To retarget standard input/output functionality, low level library functions such as fgetc() and fputc()can be re-implemented instead.

In ADS 1.2, RVDS 3.x, RVDS 4.x and DS-5, only functions such as fgetc() and fputc() need to be reimplemented. With RVDS 2.x an additional library function (used by cin) __fread_bytes_avail() must also be reimplemented.

The default version of __fread_bytes_avail() fills a buffer by calling fgetc() until either the buffer is filled or EOF is encountered.  Since the retargeted version of fgetc() in the example code simply returns characters from a UART (and never returns EOF), __fread_bytes_avail() must be re-implemented to read a single character by calling fgetc():
#ifdef __cplusplus
extern "C"
size_t __fread_bytes_avail(void *buf, size_t nbytes, FILE *stream)
/* Read a single character into buf[] using fgetc() */
  *(char *)buf = (char)fgetc(stream);
  return 1;

This function should be added to retarget.c in the Application Note 107 code \embeddedsw\Source example.

For ADS users, general retargeting of I/O functions is described in Application Note 107, "Embedded Software Development".  For RVDS users, general retargeting of I/O functions can be found in the RealView Compilation Tools documentation. For DS-5 users, see ARM Compiler toolchain documentation.


 TEXT Icon  retarget.c 

Attachments: retarget.c

Article last edited on: 2011-09-20 16:35:49

Rate this article

Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential