TruLa TruLa - 21 days ago 12
C++ Question

C++ conflicting namespaces

I am developing a

C++
project in
prj_cpp.h
which starts as

// prj_cpp.h
#include "lib_cpp.h"
extern "C" {
#include "lib_c.h"
}


Where
lib_cpp.h
and
lib_c.h
are header files of external C++ and C libraries respectively.

External C++ library
lib_cpp.h
also uses
lib_c
in the following way.

// lib_cpp.h
namespace SOME_WEIRD_NAMESPACE {
extern "C" {
#include "lib_c.h"
}
}


Because
lib_c.h
prevents double inclusion, it turns out that all objects in
lib_c.h
reside in
SOME_WEIRD_NAMESPACE
which has nothing to do with my project
prj_cpp
.

On the other hand if my header file looks like

// prj_cpp.h
extern "C" {
#include "lib_c.h"
}
#include "lib_cpp.h"


I break the external
C++
project because there is nothing under
SOME_WEIRD_NAMESPACE
because I include
lib_c.h
first.

I am not allowed to modify neither
lib_cpp.h
nor
lib_c.h


Is there anything I can do in my
prj_cpp.h
to resolve such an issue?

I do not like very much using
SOME_WEIRD_NAMESPACE
in my project because that namespace has nothing to do with it. Moreover, the number of
lib_c.h
headers files can be large.

Answer

If you use lib_cpp.h more than lib_c.h, replace #include lib_c.h with the following wrapper:

// lib_c_wrapper.h
namespace SOME_WEIRD_NAMESPACE {
extern "C" {
#include "lib_c.h"
}
}

If you use lib_c.h more than lib_cpp.h use the following wrapper:

// lib_cpp_wrapper.h
#define SOME_WEIRD_NAMESPACE YOUR_NAMESPACE
#include "lib_cpp.h"

This is a workaround to solve the problem.