3

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.

5
  • 8
    iostream internally uses stdio.h. The system call should be as same, i.e. write(2). Use strace command if you want to see all system call. Commented Mar 12, 2018 at 14:09
  • I would search the compiler's documentation. Most embedded system development chains have descriptions on how to overload the "hooks" for stream output. Commented Mar 12, 2018 at 15:42
  • 1
    That is not redirection. Redirection in the context of stdio refers to sending stdout (in this case) stream to an alternative device or file. What you are referring to is a low-level implementation dependency. Commented Mar 12, 2018 at 17:38
  • You can always create a stream that calls write and then change cout.rdbuf to that. Commented Mar 12, 2018 at 17:38
  • There is nothing like "embedded C" or "embedded C++". It depends on your (standard) libraries. But note that it is normally not advised to use the stream-IO functions/classes on typical embedded systems as they blow up the code uinnecessarily and can cause timing and memory (mostly stack) problems. Write/use simple conversion functions. Commented Mar 18, 2018 at 1:18

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 +++ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.