Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Including multiple files with same name?

Reply
Thread Tools

Including multiple files with same name?

 
 
none
Guest
Posts: n/a
 
      05-09-2010
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).
 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      05-09-2010
On May 9, 9:44*am, none <""mort\"@(none)"> wrote:
> 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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-09-2010
On 09.05.2010 15:44, * 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?


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/>)
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      05-09-2010
Jonathan Lee wrote:
> On May 9, 9:44 am, none <""mort\"@(none)"> wrote:
>> 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



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
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      05-09-2010
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
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      05-09-2010
Alf P. Steinbach wrote:
> On 09.05.2010 15:44, * 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?

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


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.
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      05-09-2010
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.
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      05-09-2010
none <""mort\"@(none)"> writes:

> Jonathan Lee wrote:
>> On May 9, 9:44 am, none <""mort\"@(none)"> wrote:
>>> 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

>
>
> 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
 
Reply With Quote
 
Jonathan Lee
Guest
Posts: n/a
 
      05-09-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to avoid including C files in other C files in an old code base Raman C Programming 5 06-07-2008 08:43 PM
"Including" multiple files? farmer ASP .Net 5 08-02-2005 04:10 AM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Including multiple xsl files from a folder Julien Phalip XML 0 02-10-2005 04:27 AM
Including Multiple xsql files Phillip XML 0 06-25-2003 09:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57