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:: cl***** 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:: cl***** 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 cl***** in the same
namespace is that later in the project lifetime there will be other
cl***** derived from the base class in file1.h and the functionality
that the collection of derived cl***** 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 cl***** 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


|