conflict between gtest.h & #include <X11/X.h>

397 views
Skip to first unread message

Ami Fischman

unread,
Jun 2, 2011, 3:49:43 PM6/2/11
to googletes...@googlegroups.com
/usr/include/X11/X.h #define's None & Bool which means that if it is #include'd before gtest.h, compilation breaks.
Concrete repro: t.cc is:
========================
#ifdef XFIRST
#include <X11/X.h>
#include "gtest/gtest.h"
#else
#include "gtest/gtest.h"
#include <X11/X.h>
#endif
========================

Command line that works: g++ -I/home/fischman/src/chromium/src/testing/gtest/include -c t.cc
Command line that fails: g++ -I/home/fischman/src/chromium/src/testing/gtest/include -c t.cc -DXFIRST
This is with g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 (on glucid).
Note that this isn't the classic macro name clash that -DGTEST_DONT_DEFINE_FOO=1 works around, because the clash here is between X.h #define'ing None (and Bool) and gtest trying to use those names (even namespace-scoped) as identifiers.

Since X.h is never going to change, and since these symbols are gtest-internal, I'm hoping gtest can be modified to use slightly less generic names.

The stderr of the failing compilation is attached.
compilation-failure.txt

Vlad Losev

unread,
Jun 2, 2011, 4:26:05 PM6/2/11
to Google C++ Testing Framework
Amy,

It shouldn't be a problem to fix None. I have logged issue http://code.google.com/p/googletest/issues/detail?id=371 to track the fix. But Bool is a part of the public API. It has been published for over 2.5 years, and by now has many users. I don't think changing it is an option.

Are you sure Bool is #defined? In the X11 installation on my workstation, Bool is typedef'ed in X11/Xdefs.h. Thus, simply including both headers should not produce compile errors WRT Bool. After that, always qualifying the use of testing::Bool in one's tests should take card of the name collisions.


The stderr of the failing compilation is attached.

Regards,
Vlad

Ami Fischman

unread,
Jun 2, 2011, 5:46:34 PM6/2/11
to Vlad Losev, Google C++ Testing Framework
But Bool is a part of the public API.

Oy.  Maybe would be a good idea to have a defensive #undef and re-#def, then.
 
Are you sure Bool is #defined? In the X11 installation on my workstation, Bool is typedef'ed in X11/Xdefs.h.

Yes, I'm sure:
$ grep Bool /usr/include/X11/X*.h|grep '#define'
/usr/include/X11/Xlib.h:#define Bool int

(I found this out by #undef'ing None and observing Bool blow up.  #undef'ing both None & Bool makes the compiler happy so at least in my config, there are no other conflicts).

Cheers,
-a

Zoltán Kurucz

unread,
Oct 19, 2015, 1:45:46 PM10/19/15
to Google C++ Testing Framework, vl...@google.com
We have ran into this problem too.
For us, Bool is defined iX11/ICE/ICElib.h

Protective #undef and #redef would be good in my opinion, but there is no standard way of redefining preprocessor symbols (those evil things...)
There is a non-standard solution, which seems to be widely supported: http://stackoverflow.com/questions/1543736/how-do-i-temporarily-disable-a-macro-expansion-in-c-c
Don't know if these kind of things can be used in gtest.

In my opinion, this should also be fixed in the X11 headers, by undeffing them at the end of the header. This might not be possible, because these defines are globally visible, so they have to be regarded as public API I think...

Regards,
Zoltán
Reply all
Reply to author
Forward
0 new messages