2

I have a library Lib and an executable App with the following file hierarchy:

Lib/
  - external/
       - pugixml/
            - pugixml.hpp -> namespace ``pugi`` and header guard ``LIBA_PUGIXML_HPP``
            - pugixml.cpp 
App/
  - external/
       - pugixml/
            - pugixml.hpp  ->  namespace ``pugi`` and header guard ``APP_PUGIXML_HPP``
            - pugixml.cpp 

Both App and Lib use the external library pugixml (might be different versions). They include thes files directly in the project and pugixml is no external dependency anymore basically.

App depends on Lib and includes all necessary sources from Lib (it does not link with Lib) to successfully compile itself.

How can I avoid namespace clashes of the pugixml.hpp library which arises when compiling App. How can I use two different versions of the same library in this setting?

I see the following options:

  • Move namespaces pugixml in A into namespace A{ namespace pugixml {...} }, and the same in app : move namespaces pugixml in app into namespace app{ namespace pugixml {...} }

  • Rename all namespaces ? How should I do this? Hardcoding? or is there a c++ syntax for this?

What is a good approach?

Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • 2
    A possible third solution: Don't include the pugixml files from the A library in your project? Or use the pugixml files *from* the library, and don't include yours? – Some programmer dude Jun 18 '15 at 13:32
  • 1
    If it's the same library why are the include guards different? – Alex Jun 18 '15 at 13:36
  • the include guards are different (i dont know) to really say that pugixml.hpp belongs to ``A`` or ``app``... (hm...) How can I use library ``A`` from source in appplication ``app`` both having their own ``pugixml`` sources, and not having compile errors – Gabriel Jun 18 '15 at 13:47
  • The include guards are just there to protect the header file from being included in the same source file multiple times, nothing else, You can use either your files or the files from the library without worrying, at least if they are compatible (i.e. about the same version). – Some programmer dude Jun 18 '15 at 13:51
  • @Gabriel Except pugixml is an independant project, so it does not really "belong" to any of those projects. Either you flatten the whole thing by compiling all sources into an executable, or you use proper dependency management by linking your library and pugixml to your executable. In either case, do not modify the pugixml code. – SirDarius Jun 18 '15 at 13:52
  • @ SirDarius, I would like that lib ``A`` has its own pugixml sources. and ``app`` has its own pugixml source. Of course i could link ``app`` and lib ``A`` both with one compiled library ``pugixml`` but that is annoying because this library is really small and can just be added to the projects, but how to do this? – Gabriel Jun 18 '15 at 13:58
  • @ Joachim, but what happens when they have different versions? and are not compatible. Basically I could make pugixml a dependency for ``A`` and for ``app`` and both compiling/linking them with one pugixml library.. but I dont want additional dependencies – Gabriel Jun 18 '15 at 14:03
  • I updated the question, as it was a little bit unclear I think – Gabriel Jun 18 '15 at 14:17
  • 1
    Crack open both App and Lib, and make pugixml their proper external dependency. Having private copies of dependencies is **just plain wrong and doesn't work**. – n. m. could be an AI Jun 18 '15 at 14:40

0 Answers0