I want to retarget the fputc library function so that I can have it both write to a uart (when used with printf) and write to a file (when used with fwrite). The override symbol feature of the compiler and linker seemed like just the thing for this. So, I added the following code to my retarget.c file, per the manuals. I already had a sendchar routine to do I/O to the UART.
extern int $Super$$fputc(int c, FILE *fp); // original library function int $Sub$$fputc(int c, FILE *fp) // override for original { if (fp == stdout) { return sendchar(c); // output to uart } else { return $Super$$fputc(c, fp); // call original for file I/O } }
The problem is, this code never gets called. It compiles and links just fine, but it never gets called. In this case my file I/O will work but my printf will not work (since I assume I am calling the original library routine).
If I include my own fputc routine, in retarget.c or some other module, THEN the above code will be called. In that case it correctly does the "if" check on stdout. If the routine was called by printf then sendchar will be called and the I/O will go to the uart. But when the $Super$$fputc is called it ends up going to my local copy of fputc and NOT the library routine, which is not what I want and of course the file I/O does not work. So, I can either have my printf work, or my file I/O, but not both. Why does this not work as it is supposed to?