user82375897897928347
user82375897897928347

Reputation: 95

C++ stream redirection

In embedded C, i use printf which redirects to the system call "_write" which allows my to overload _write and redirect to Uart or Usb VCP.

Now in embedded C++ i would like to do the same for std streams std::cout std::cin.

Where do the calls lead to? where do i end up when calling cout/cin?? is there also a system call which i may overload?

printf("hi") --> _write()
std::cout << "hi" --> ????????????

Since i cannot debug standart library calls, i do not know what happens there.

if someone has experience with this, please give me some examples and tipps.

Upvotes: 3

Views: 1091

Answers (2)

Werner
Werner

Reputation: 281

Credit goes to liliscent. strace of code above shows that both calls printf and cout end up in the same write system call.

...
brk(0x775000)                           = 0x775000
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 17), ...}) = 0
write(1, "hihi", 4hihi)                     = 4
exit_group(0)                           = ?
+++ exited with 0 +++

Upvotes: 0

Clifford
Clifford

Reputation: 93476

Most standard C++ libraries are implemented using the underlying C library (which is itself a subset of the C++ library in any case).

It is not usual for the C++ library to require a separate retargetting layer to the C library.

You do not need access to the library source to demonstrate this. You can simply place a break-point at _write (in your case - that symbol is by no means a given), and then run the cout code to demonstrate that it is implemented using the _write syscall.

Upvotes: 2

Related Questions