Including multiple files with same name?

N

none

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?

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).
 
J

Jonathan Lee

But how do I include multiple files with the same name?

#include "A/X/x.h"
#include "B/X/x.h"

Adjust your compiler's include path accordingly.

--Jonathan
 
A

Alf P. Steinbach

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?

The language standard is silent on the issue of how an #include path specifies a
header file. But in practice all compilers (including Windows compilers) accept
the *nix convention of writing

#include "A/X/x.h"

or

#include <A/X/x.h>

The standard does however specify the general difference between "..." and
<...>. Namely that the former may look in some additional places first. And if
that fails, the search continues as with the latter form.

In practice, at least when you only specify a filename, with current compilers
the additional search for the "..." form includes to look in the directory of
the file where the #include directive appears.

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).

In C++ namespaces have no other relation to directories than what you establish
yourself. In general it's not a good idea to couple these concepts too tightly.
Too tight coupling between logical packaging (namespaces) and physical packaging
(directories) can lead to "namespace hell", as well as "directory hell" --
borrowing some Microsoft terminology (they invented "DLL hell").


Cheers & hth.,

- Alf
(blog at <url: http://alfps.wordpress.com/>)
 
N

none

Jonathan said:
#include "A/X/x.h"
#include "B/X/x.h"

Adjust your compiler's include path accordingly.

--Jonathan


But in my main application I have a switch where I select between the two types so they both need to
be available when I compile my program.

But to do this it seems that its necessary to have unique filenames like:

A/X/a_x.h

B/X/b_x.h
 
P

Paul Bibbings

none said:
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
 
N

none

Alf said:
The language standard is silent on the issue of how an #include path
specifies a header file. But in practice all compilers (including
Windows compilers) accept the *nix convention of writing

#include "A/X/x.h"

or

#include <A/X/x.h>

The standard does however specify the general difference between "..."
and <...>. Namely that the former may look in some additional places
first. And if that fails, the search continues as with the latter form.

In practice, at least when you only specify a filename, with current
compilers the additional search for the "..." form includes to look in
the directory of the file where the #include directive appears.



In C++ namespaces have no other relation to directories than what you
establish yourself. In general it's not a good idea to couple these
concepts too tightly. Too tight coupling between logical packaging
(namespaces) and physical packaging (directories) can lead to "namespace
hell", as well as "directory hell" -- borrowing some Microsoft
terminology (they invented "DLL hell").


Cheers & hth.,

- Alf
(blog at <url: http://alfps.wordpress.com/>)

Yes I begin to realize that. I started using a naming of my classes corresponding to the filenames
and the namespace.

Currently I make sure each filename is unique and then I use the namespaces to create another
ordering which is more intuitive when developing the code.
 
N

none

Paul said:
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!


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.
 
P

Paul Bibbings

none said:
But in my main application I have a switch where I select between the
two types so they both need to be available when I compile my program.

But to do this it seems that its necessary to have unique filenames like:

A/X/a_x.h

B/X/b_x.h

Can you provide a compilable example of how this `switch' causes a
problem in the face of the inclusion strategies suggested already.

Regards

Paul Bibbings
 
J

Jonathan Lee

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top