If printf is not present in main, then both functions will not get
called for stdin and stdout and I think I now know why. In the ustl
package in ofstream.cpp the streams are not created because the streams
(cout, cin, cerr) are declared this way:
ifstream cin (STDIN_FILENO);
ofstream cout (STDOUT_FILENO);
ofstream cerr (STDERR_FILENO);
That means they use the fileno to open the streams - they do not create
them. But the streams are not present, because no function uses stdout,
stdin, that means there are no references and the linker throws stdout
and stdin away. This is the reason why the static constructors of stdout
and stdin are never get called.
Here is my proposal to solve this problem. I changed the declaration of
cout, cin and cerr this way:
ifstream cin (((Cyg_StdioStream *)stdin)->get_dev());
ofstream cout (((Cyg_StdioStream *)stdout)->get_dev());
ofstream cerr (((Cyg_StdioStream *)stderr)->get_dev());
This ensures that stdout, stdin and stderr are created. After this
change cyg_fd_assign() and cyg_fd_alloc() will get called if there is no
printf function in main.cpp. The call cout << "Hello world!\n" works
now. I noticed that the message is already printed before cout.flush()
is called. So it seems cout.flush() is not required if a proper serial
driver is used. If I use the diagnostic serial driver, then I always
needed to call cout.flush().
If you agree to my proposal then I will provide a patch.