tpounds / tpunitpp

A simple, portable C++ xUnit library contained in a single header.
MIT License
23 stars 8 forks source link

Can't use cstdio with tpunit. #4

Closed c0yote closed 9 years ago

c0yote commented 9 years ago

I can't build with both tpunit++ and cstdio. This is happening with MinGW 4.9.2

Simply adding #include <cstdio> to the top of tpunit++main.cpp breaks it with the following:

In file included from tpunit++main.cpp:23:0:
tpunit++.hpp:29:39: error: conflicting declaration of 'int printf(const char*, ...)' with 'C' linkage
 extern "C" int printf(const char*, ...);
                                       ^
In file included from C:/mingw-w64/mingw64/x86_64-w64-mingw32/include/c++/cstdio:42:0,
                 from tpunit++main.cpp:22:
C:/mingw-w64/mingw64/x86_64-w64-mingw32/include/stdio.h:296:5: note: previous declaration with 'C++' linkage
 int printf (const char *__format, ...)
     ^

Removing line 29: extern "C" int printf(const char*, ...);

works with the cstdio include, but I don't want to hand edit the header every time I update. I don't know if this happens on other compilers\platforms, but I would find it hard to imagine it doesn't.

I don't have a good solution for this. Maybe check for cstdio already turned on?

e.g.

#ifndef __CSTDIO_HEADER
    extern "C" int printf(const char*, ...);
#endif

The block above did compile.

Or maybe a preprocessor flag to allow us to turn off the extern, and enabling standard cstdio through the include?

e.g.

#ifndef TPUNITPP_SUPPORT_CSTDIO
    extern "C" int printf(const char*, ...);
#else
    #include <cstdio>
#endif
tpounds commented 9 years ago

@c0yote Thanks for the report. It appears from the error message that the MinGW error is a result of a C vs. C++ symbol mangling issue. I can't reproduce the issue using standard glibc on Linux since it's a C library. I am able to synthetically reproduce the problem by declaring a pseudo-printf function inline though. That said, I can't remember what obscure compiler issue I ran into when originally inlined the printf declaration (been around since the initial import). I'll poke around with some of the more obscure compilers in the list and see if I can reproduce what issue I was originally guarding against. If nothing breaks I'll just replace the inline declaration with a #include <cstdio>.

c0yote commented 9 years ago

Thanks, for having a look.