1

I have to override the standard printf function and implement it in a user-specific way. I have tried it as follows:

extern "C" int printf(const char* format, ...)
{
    // user specific implementation
}

With gcc, this code can be built. Visual Studio aborts the build with the following error message:

Error C2084 function 'int printf(const char *const ,...)' already has a body

Thanks you for any hints

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Markus F
  • 25
  • 3
  • 2
    _Why_ exactly do you have to do this? What problem are you trying to solve? – Human-Compiler Apr 20 '21 at 18:47
  • It's C++ code that uses printf to create a test monitor interface. This printf outputs must be forwarded to the user interface (.net core wpf app) in a Windows simulation. – Markus F Apr 20 '21 at 19:04
  • As I understand it, functions from the standard library would only need to be linked if they do not exist in the project. At least that's how it's handled in gcc. – Markus F Apr 20 '21 at 19:04
  • 1
    Functions are not like `virtual` class methods. You can't *override* functions, only *overload* them, or possibly *detour* them. – Remy Lebeau Apr 20 '21 at 19:17
  • 6
    Create your own `my_printf()` function and then use `int my_printf(const char *const ,...); #define printf my_printf` in files you want to override the default. – chux - Reinstate Monica Apr 20 '21 at 19:18
  • I don't see how it can be possible to do what you are hoping to do without also disabling linking and use of the C standard library. Even then, I suspect that trying to define these symbols is likely a form of undefined behavior -- and so `gcc` compiling this successfully is likely just coincidental – Human-Compiler Apr 20 '21 at 19:27
  • From what I can find, any definition of a C-standard library symbol violates ODR and also is using an identifier and signature that is reserved by the C standard library -- which would be undefined behavior ([source](https://stackoverflow.com/a/58595833/1678770)). Your best bet is to define a custom function and use that – Human-Compiler Apr 20 '21 at 19:30
  • You are not allowed to provide your own version of any standard library functions (with a few exceptions not relevant here). You **might** get away with it through linker games, but that is not portable. – Pete Becker Apr 20 '21 at 21:38
  • @PeteBecker Can you provide a source for that claim? All linkers I know only include definitions of not-yet-resolved references, and this is commonly well defined and documented. – the busybee Apr 21 '21 at 06:32
  • And that's how I would have expected it from Visual Studio! – Markus F Apr 21 '21 at 07:34
  • @Human-Compiler: gcc provides the functions as [**weak** symbols](https://en.wikipedia.org/wiki/Weak_symbol) to allow user replacement. – Jarod42 Apr 21 '21 at 08:33
  • 1
    You could start your application from another one in a pipe with something like `popen` and reroute the error output channel to the regular one. That way, the output lands in a buffer instead of the console and you can do whatever you want. A kind of convoluted solution, but if you are interested, I can post some code. – Aziuth Apr 21 '21 at 08:42
  • I would try rerouting the output at the file descriptor level, or even start the C++ part in a separate process rather than (I assume) P/Invoking it. Fiddling with source-level output functions sounds like a game of whack-a-mole to me. – Quentin Apr 21 '21 at 08:42
  • there's no weak symbol in MSVC – phuclv Apr 21 '21 at 08:43
  • If you really want to do this, you have to hook the API. – prehistoricpenguin Apr 21 '21 at 10:39

0 Answers0