10

I am using a C library (libgretl) from C++ and some of its functions conflict with my code, so I wanted to wrap it in a namespace, like this:

namespace libgretl {
extern "C" {
    #include <gretl/libgretl.h>
}}

However, this does not compile, I get "undefined" errors from gcc files (using mingw32 with gcc 4.5.2 on Windows). The first errors come from the following code block of file c++/cstddef:

_GLIBCXX_BEGIN_NAMESPACE(std)
  using ::ptrdiff_t;
  using ::size_t;
_GLIBCXX_END_NAMESPACE

where the macros expand respectively to namespace std { and }. There are more errors after these.

Omitting the extern "C" directive does not help. Using an anonymous namespace reduces the amount of errors, but it still won't compile.

My question is therefore if there is some way to include such a C library and place its functions into a namespace, without changing the gcc or the library's source files?

Thanks.

Michal

2
  • The short answer is no I think Commented Aug 25, 2011 at 9:41
  • 1
    The problem is presumably that gretl/libgret1.h is including stddef.h, and so you are indirectly including a standard header inside a namespace, which is a no-no.
    – ndkrempel
    Commented Jul 17, 2012 at 0:21

3 Answers 3

7

You can't do it. Namespaces are not just source code decorations, they are mangled to object symbols by compiler.

Native C function foo() in library will be available by symbol _foo in object file, but calling bar::foo() will generate reference to, for example, @N3barfoo. As result, linker error will occur.

You may create "proxy" functions in separate source file, including original library header only in this source and putting all proxy functions to namespace.

4
  • I think I understand what you are saying, but it does not explain two things: why does it work with another C library (my own, much simpler than libgretl)? An why does it fail on compile - would not the name mangling issues arise first in the linker? Commented Aug 25, 2011 at 11:22
  • Try looking at preprocessed source, gcc will output it with -E switch. Possibly there are some #defines in C header which breaks later compilation (#define namespace ..., f.e.)
    – blaze
    Commented Aug 25, 2011 at 11:41
  • no, namespaces are only used (like in the original post), never defined or otherwise broken Commented Aug 25, 2011 at 12:00
  • Then I have no ideas :( Guess 'extern "C" inside namespace' isn't very well implemented in compilers.
    – blaze
    Commented Aug 26, 2011 at 10:14
3

You don't get to simply wrap a namespace around an external declaration and have it appear within that namespace... the item (function, global) must have been built within that namespace from the start. Since C doesn't support namespace resolution, this could not have been the case.

You need to change your own code to accommodate this library, unless you're willing to chante the library itself.

In order to refer to a non-namespace'd item that conflicts with your own namespace'd item, refer to ::item().

1

I guess the C library was compiled as C, which means namespaces are not included and not supported in the compiled code. Thus, your compiled C library cannot be in a namespace. Altering the header by encapsulating the include will not change that.

You can still encapsulate your own code in a namespace.

4
  • Strange - I do this for another C library and it works there. I naively thought that the C code "won't see" the namespace, i.e. that it would affect only the encapsulating C++ code.. Commented Aug 25, 2011 at 9:48
  • As for encapsulating my own code in namespaces - I do this, by some of the library's definitions conflict with my namespace names :-( Commented Aug 25, 2011 at 9:49
  • @Michal Kaut: Then what you need is to specify which function you want called: ::foo(); will refer to foo() in the global namespace, while foo(); will pickup the first match according to the usual rules. Commented Aug 25, 2011 at 11:49
  • @dribeas Not sure: the library defines a constant named PCA, while my code has a namespace called PCA; compilation therefore fails at the moment it reaches the second definition, i.e. before I ever use any of the two objects. Commented Aug 25, 2011 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.