On May 9, 10:16*am, none <""mort\"@(none)"> wrote:
> Paul Bibbings wrote:
> > none <""mort\"@(none)"> writes:
>
> >> I have the following structure in a src folder:
>
> >> A/X
> >> A/X/x.h
> >> A/X/x.cpp
> >> B/X
> >> B/X/x.h
> >> B/X/x.cpp
>
> >> where A/X/x.h is:
>
> >> * * * *#ifndef A_X_H
> >> * * * *#define A_X_H
>
> >> * * * *namespace A {
> >> * * * * *class X {
>
> >> * * * * *};
> >> * * * *}
> >> * * * *#endif
>
> >> And:
>
> >> where B/X/x.h is:
>
> >> * * * *#ifndef B_X_H
> >> * * * *#define B_X_H
>
> >> * * * *namespace B {
> >> * * * * *class X {
>
> >> * * * * *};
> >> * * * *}
> >> * * * *#endif
>
> >> But how do I include multiple files with the same name?
>
> > * *15:09:28 Paul Bibbings@JIJOU
> > * */cygdrive/d/CPPProjects/CLCPP/include_multiple $cat A/X/x.cpp
> > * *// file: x.cpp
>
> > * *#include "x.h"
>
> > * *void A_f()
> > * *{
> > * * * A::X ax;
> > * *}
>
> > * *15:09:36 Paul Bibbings@JIJOU
> > * */cygdrive/d/CPPProjects/CLCPP/include_multiple $cat B/X/x.cpp
> > * *// file: x.cpp
>
> > * *#include "x.h"
>
> > * *void B_f()
> > * *{
> > * * * B::X bx;
> > * *}
>
> > * *15:09:44 Paul Bibbings@JIJOU
> > * */cygdrive/d/CPPProjects/CLCPP/include_multiple $cat main.cpp
> > * *// file: main.cpp
>
> > * *#include "A/X/x.h"
> > * *#include "B/X/x.h"
>
> > * *int main()
> > * *{
> > * * * A::X ax;
> > * * * B::X bx;
> > * *}
>
> > * *15:09:49 Paul Bibbings@JIJOU
> > * */cygdrive/d/CPPProjects/CLCPP/include_multiple $g++ main.cpp
> > * * * A/X/x.cpp B/X/x.cpp
>
> > * *15:10:07 Paul Bibbings@JIJOU
> > * */cygdrive/d/CPPProjects/CLCPP/include_multiple $
>
> > No conficts!
>
> >> I guess this should be possible since that is the whole point with
> >> namespaces (to have multiple types with the same name but located in
> >> different namespaces).
>
> > Note: your `point' about namespaces here is a little irrelevant, since
> > the workings of namespaces set no requirement on the names of the files
> > that those namespaces, etc., are defined in.
>
> > Regards
>
> > Paul Bibbings
>
> I would like to be able to just write:
>
> #include "x.h"
>
> and not:
>
> * #include "A/X/x.h"
> * #include "B/X/x.h"
>
> but now I understand that this is of course *impossible.
If you want #include "x.h" to find *both* that won't work.
If you mean you only want to include one or the other (not
both) then you have a couple of options.
The first would be to make an "x.h" that includes one or
the other based on some configuration option. Like
// mutex.hpp - selects the appropriate OS mutex
#ifdef __linux__
# include "linux/mutex.hpp"
#else
# include "windows/mutex.hpp"
#endif
(might not be "__linux__"... it's just an example).
This site can be a help for such a setup:
http://predef.sourceforge.net/
The second option would be to use your toolchain to
add the correct directories to the include path.
For example, GCC uses something like
gcc -IA/X ...
to add the directory A/X to the include path. In this
way #include "x.h" will refer to A/X/x.h. You can
set up a Makefile to switch between the two. For that
I would consult the documentation of your toolchain.
Personally, I prefer the former.
--Jonathan