Including multiple files with same name?

Discussion in 'C++' started by none, May 9, 2010.

  1. none

    none Guest

    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).
    none, May 9, 2010
    #1
    1. Advertising

  2. none

    Jonathan Lee Guest

    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
    Jonathan Lee, May 9, 2010
    #2
    1. Advertising

  3. 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/>)
    Alf P. Steinbach, May 9, 2010
    #3
  4. none

    none Guest

    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
    none, May 9, 2010
    #4
  5. 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
    Paul Bibbings, May 9, 2010
    #5
  6. none

    none Guest

    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.
    none, May 9, 2010
    #6
  7. none

    none Guest

    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.
    none, May 9, 2010
    #7
  8. 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
    Paul Bibbings, May 9, 2010
    #8
  9. none

    Jonathan Lee Guest

    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
    Jonathan Lee, May 9, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SamIAm
    Replies:
    2
    Views:
    359
    SamIAm
    Oct 29, 2003
  2. farmer

    "Including" multiple files?

    farmer, Aug 2, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    581
    farmer
    Aug 2, 2005
  3. Phillip
    Replies:
    0
    Views:
    1,180
    Phillip
    Jun 25, 2003
  4. Replies:
    4
    Views:
    952
    M.E.Farmer
    Feb 13, 2005
  5. Moskie
    Replies:
    0
    Views:
    388
    Moskie
    Aug 4, 2006
Loading...

Share This Page