Many times I'd like to replace libc functions in embedded systems, because of some tests or because I need a different implementation.
For example, sprintf implementation of newlib uses malloc and I can't use it many times.
It sometimes happens that I can use malloc, with some restrictions. Just for test or optimization, I replace libc malloc with my own implementation.
In these cases I use a very simple trick: I simply define a new sprintf()/malloc() function in one of my source code. During linking (GNU ld), the linker prefers my implementation and ignore the implementation in the libc. I don't know why this happens and if it is a standard way during linking, but it works for me.
Recently I found a different behaviour, with an error from the linker: multiple definitions of sprintf.
After some time, I understood it is caused by a piece of code that uses another libc function (ctime) that uses sprintf too, I suppose. Maybe in this case, ctime needs sprintf from libc so the linker tries to add two sprintf and gives the error.
First question: what exactly happens?
Second question: is there a better method to override a standard libc function? I know I can use preprocessor magic, but in this case I should write MYSPRINTF(...) that is much worse than sprintf(...).