The Linux <ncurses.h>
header defines the function meta
, and the C++ metaprogramming library meta
puts all its code in the global namespace meta
.
How can I use both in the same C++ program (not necessarily the same TU but that would be nice)? 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 ncurses
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 meta
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 ::meta::symbol_name
for that, this approach will break.
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.
I'm reasonably certain that neither A nor B will actually work, at least as given. You've pointed toward one of them, but I think it's the less likely of the two. There are two problems that are basically mirror images of each other.
If the code in ncurses
is declared as extern "C"
(typical for many C libraries that have been made to work with C++), surrounding them with a namespace won't actually work--an extern "C"
declaration basically ignores namespaces and declares a function in the global namespace. The namespace won't change much of anything, and you'll still have a collision.
If the content of <ncurses.h>
is not declared extern "C"
, then you'll run into the problem you cited: the library is built with functions in the global namespace, but the client code is seeing definitions for code in the linux
namespace. Since the namespace affects the mangled name (that's how it prevents a collision) your code won't be able to link. All the linux::*
functions will show up as unresolved externals.
To make this work, you need to assure that none of the library code is declared extern "C"
, and specify the namespace inside the header (and the library source files), and re-compile the library with these declarations, so the library and its client code agree on the namespace where that code resides.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With