confusion defining a namespace

P

Pep

Namespaces still confuse me when I try to get clever :(

Can I do the following

// file1.h
#ifndef __NS1_H__
#define __NS1_H__

namespace ns1
{
some base class
}
#endif // __NS1_H__

// file file1.cc
#include <file1.h>
implementation of ns1:: classes defined in file1.h

// file2.h
#ifndef __NS2_H__
#define __NS2_H__

#include <file1.h>

namespace ns1
{
some derived class
}
#endif // __NS2_H__

// file2.cc
#include <file2.h>
implementation of ns1:: classes defined in file2.h

then compile file1.cc and file2.cc in to a library called libns1.a

file1.h defines a base class that is used in a derived class defined
in file2.h , so I obviously need to include file1.h in to file2.h.

The reason I have both the base and derived classes in the same
namespace is that later in the project lifetime there will be other
classes derived from the base class in file1.h and the functionality
that the collection of derived classes provide is considered to be a
unique module within the business model, so from a development point
of view it would be ideal if all derived classes sit within the same
namespace. It does not feel right to have to define a new namespace
for each derived class.

I do not want to put the base class and all derived class definitions
in the same header file as that means that everything will need to be
recompiled each time a new derived class is defined. In our already
lengthy build process, adding more time by haveing to rebuild all
derived class implementation files just because we added a new one is
not desired.

I tried something like this and whilst the library libns1.a is built
okay (I say this because there are no errors in the build process) I
get problems when I try to use the library, as follows

//file main.cc
#include <file2.h>

int main(int argc, char** argv)
{
// do something
}

When I compile main.cc and link against libns1.a, I get the following
error

libns1.a(file2.o) multiple definition of `namespaces' testns1.o:(.data
+0x0): first defined here

So where am I going wrong, is it that it is simply not possible to
extend the definition of a namespace across header files?

I am building this on a linux o/s with g++ 3.3.6

TIA,
Pete
 
V

Victor Bazarov

Pep said:
Namespaces still confuse me when I try to get clever :(

Can I do the following

// file1.h
#ifndef __NS1_H__
#define __NS1_H__

No, you cannot. Identifiers with double underscores in them
are reserved by the implementation.

V
 
P

Pep

Victor said:
Pep said:
Namespaces still confuse me when I try to get clever :(

Can I do the following

// file1.h
#ifndef __NS1_H__
#define __NS1_H__

No, you cannot. Identifiers with double underscores in them
are reserved by the implementation.

V

Erm, okay so apart from that, which actually does work but I'll
concede the point, assuming those tags are something else like
XXX_FILE1_H_XXX, the real question is about spreading a namespace over
multiple headers and implementation files.

I've answered my own question in the meantime, because I was pretty
sure I was correct. So I extracted the simplistic example I posted out
to it's own area and build a library from the 2 headers and 2 source
files, with an additional source file containn a main function to test
it.

As I expected, the library was correctly built with the namespace
spread over multiple source files.

However it brought me straight back to my original problem which is
what prompted my original post. Although the library builds, I get an
error stating that the symbols I am using in the main program which
are located in the library are undefined :(

I know the whole thing works if I compile all the source files in a
single unit, so the namespace is not an issue. I have dumped the
symbol table form the library using nm and compared the mangled names
to the ones in the object file containing main routine and the match.

So I don't know what to do now but am happy that my original post is
solved.

Yes I agree with your comment about the double underscores but
unfortunately this application is many years old, badly designed and
written in the first place so you kinda have to run with it and pray
that we never hit a situation where we have a clash. Though I would
secretly be happy if that did happen because then I would be given the
required time to clean the code up.

lol, it leads you to doubt yourself, hence my testing of spreading a
namespace over multiple source files, even though I know std:: is
spread over multiple files :)

Thanks for you help though,
Pete
 
V

Victor Bazarov

Pep said:
[..] the real question is about spreading a namespace over
multiple headers and implementation files.

AFAIK, there is no issue with that. I.e. the language allows it,
and the compilers support it. Perhaps your compiler (linker) is
somehow broken? Have you tried your test case with the most up to
date GNU compiler?
I've answered my own question in the meantime, because I was pretty
sure I was correct. So I extracted the simplistic example I posted out
to it's own area and build a library from the 2 headers and 2 source
files, with an additional source file containn a main function to test
it.

As I expected, the library was correctly built with the namespace
spread over multiple source files.

However it brought me straight back to my original problem which is
what prompted my original post. Although the library builds, I get an
error stating that the symbols I am using in the main program which
are located in the library are undefined :(

Here I have nothing else to tell you except post your _complete_ of
your test case here (five files, supposedly), and perhaps duplicate
your question in the GNU G++ newsgroup (gnu.g++.help).
I know the whole thing works if I compile all the source files in a
single unit, so the namespace is not an issue. I have dumped the
symbol table form the library using nm and compared the mangled names
to the ones in the object file containing main routine and the match.

I take it it was a typo and you meant ".. and they match".
So I don't know what to do now but am happy that my original post is
solved.

[..]

V
 
P

Pep

Victor said:
Pep said:
[..] the real question is about spreading a namespace over
multiple headers and implementation files.

AFAIK, there is no issue with that. I.e. the language allows it,
and the compilers support it. Perhaps your compiler (linker) is
somehow broken? Have you tried your test case with the most up to
date GNU compiler?
I've answered my own question in the meantime, because I was pretty
sure I was correct. So I extracted the simplistic example I posted out
to it's own area and build a library from the 2 headers and 2 source
files, with an additional source file containn a main function to test
it.

As I expected, the library was correctly built with the namespace
spread over multiple source files.

However it brought me straight back to my original problem which is
what prompted my original post. Although the library builds, I get an
error stating that the symbols I am using in the main program which
are located in the library are undefined :(

Here I have nothing else to tell you except post your _complete_ of
your test case here (five files, supposedly), and perhaps duplicate
your question in the GNU G++ newsgroup (gnu.g++.help).
I know the whole thing works if I compile all the source files in a
single unit, so the namespace is not an issue. I have dumped the
symbol table form the library using nm and compared the mangled names
to the ones in the object file containing main routine and the match.

I take it it was a typo and you meant ".. and they match".
So I don't know what to do now but am happy that my original post is
solved.

[..]

V

Yep it has turned in to a g++ help question now that I have proved to
myself that which I already knew :)

I have posted a complete test case in gnu.g++.help and rather then
report it here (quite long) I've included the link for you.

http://groups.google.co.uk/group/gnu.g++.help/browse_thread/thread/ff6b83b731f32051#4b4c30f1f991d1a8

Thanks for your help, at least I know I am not insane with spreading
namespaces, lol
 
R

red floyd

pelio said:
Victor Bazarov a écrit :

Is it really forbidden by the standard ? if it is, should not have an
error ?

Or perhaps it is allowed but not advisable ?

Nope, such identifiers are reserved to the implementation (*you* may not
use them, but the implementation can):

See 17.4.3.1.2

"Each name that contains a double underscore (__) or begins with an
underscore followed by an upper-case letter (2.11) is reserved to the
implentation for any use."
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top