0

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.

6
  • 2
    lib_cpp.h is broken. This is the fundamental problem, and fixing it is the right fix. Commented Nov 17, 2016 at 18:01
  • Agreed, what would be the proper fix? Prevent using lib_c.h under namespaces? Commented Nov 17, 2016 at 18:06
  • write new, non-broken versions of lib_cpp.h and lib_c.h and use them instead of the originals. The originals are unmodified, so the assignment spec is still met. Commented Nov 17, 2016 at 18:08
  • OK, how the non-broken version of lib_cpp.h should look like? Never use C header files under the namespace clause? Commented Nov 17, 2016 at 18:20
  • Header files are not libraries! Commented Nov 17, 2016 at 19:12

2 Answers 2

2

Since lib_cpp.h and lib_c.h are from third parties and new versions of those libraries might provide new, different versions of those files, it will avoid future maintenance issues if you don't alter them. Also you state that your specification is that you don't alter them. So wrap them and exclusively use the wrappers in your code to resolve the issue.

The first approach that occurs to me is:

//wrapped_lib_c.h #ifndef WRAPPED_LIB_C_INC_GUARD #define WRAPPED_LIB_C_INC_GUARD namespace SOME_WEIRD_NAMESPACE { extern "C" { #include "lib_c.h" } } namespace SOME_PROJECT_APPROPRIATE_NAMESPACE { using SOME_WEIRD_NAMESPACE; } #endif 

and

//wrapped_lib_cpp.h #ifndef WRAPPED_LIB_CPP_INC_GUARD #define WRAPPED_LIB_CPP_INC_GUARD #include "lib_cpp.h" namespace SOME_PROJECT_APPROPRIATE_NAMESPACE { using SOME_WEIRD_NAMESPACE; } #endif 

Some variation of this should work for your situation and the details that you need to maintain are isolated in your wrapping files.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.