Gtkmm.h leaks into the global namespace

First, I hope I have found the new home for GTKMM discussion.

I have discovered that if I create a class called “pipe,” somewhere in gtkmm.h this is already declared, and my program can’t compile:

#include <gtkmm.h>
  
  class pipe
  {
      public:
          pipe() : interactionState(true) { };
      protected:
          bool interactionState;
  };
  
  int main()
  {
      pipe foo;
      return 0;
  }
48> g++ `pkg-config --cflags gtkmm-3.0` -o pipe pipe.cpp 
pipe.cpp: In function 'int main()'
pipe.cpp:13:9: error: expected ';' before 'foo'
   13 |     pipe foo;
      |         ^~~~
      |         ;

I either have to remove the include to gtkmm.h, or in main declare foo with “class pipe foo.”

I was not expecting that GTKMM adds anything to the global namespace, and that everything is declared in one of the GTKMM specific namespaces. Here are my questions:

Is this expected, or does this seem to be a mistake, something that slipped through but which ought to be found under Gtk::pipe, for example? Please note that I am using GTKMM 3.24.7.

If this is expected or not intended to be fixed, is there a way that I can encapsulate this and any other leaked names back into Gtk or some other namespace?

Thanks for your time.

I guess <gtkmm.h> brings in <unistd.h> which defines pipe.

gtkmm.h includes a lot of stuff, for instance (indirectly) GLib’s header files. One of those files is gio/gcredentials.h, which includes unistd.h.

Thanks for the info. I’ve wrapped my code in a namespace.

I was thinking that, ideally, making use of a library should never pollute the global namespace. But now I’m not sure that is even possible. For example, let’s say that the stuff that GTKMM needs to include which isn’t part of the direct API were all put into a separate namespace, maybe __GTKMM__. Then, I think due to #include guards, if some user of this library were to need to use another library already included in GTKMM, their could would silently not include it, but also it would not be available in the global namespace, or whatever namespace it normally is in because GTKMM put it under its own namespace.

Long story short, I guess there’s no elegant solution here.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.