gnzlbg gnzlbg - 1 year ago 130
Linux Question

Name collision between c++ library namespace and C linux function

The Linux

header defines the function
, and the C++ metaprogramming library
puts all its code in the global namespace

How can I use both in the same C++ program? Is there a way to work around the name collision?

I can think of two brittle workarounds, but they are easy to break:

  • Workaround A:

    namespace linux {
    #include <ncurses.h>
    } // namespace linux
    using linux::max_align_t; // ncurses assumes it is in the global namespace
    #include <meta/meta.hpp>

    compiles but will probably fail to link since the
    symbols are expected in the global namespace.

  • Workaround B:

    #include <ncurses.h>
    namespace cpp {
    #include <meta/meta.hpp>
    } // namespace cpp

    is very brittle since it will only work as long as the
    library doesn't assume that any of its symbols are in the global namespace. That is, if the library needs to disambiguate internally a symbol and uses
    for that, this approach will break.

Answer Source

I would suggest workaround C: Isolate your code such that the meta library use and the ncurses use are in separate translation units in your project. This way in any particular translation unit there isn't one symbol being used as both a namespace and a global function.