namespace difficulties (many files)

This is a discussion on namespace difficulties (many files) within the C++ Programming forums, part of the General Programming Boards category; How can I get namespaces to span multiple files?
If I do a
namespace blah
{
// ....
}
in ...

namespace difficulties (many files)

in one file, then try and use it in another I get compiler errors that it doesn't know what the namespace is, and I've tried preceeding it with extern, and it's contained variables.

If I define the namespace in a header, the compiler gives me multiple definition errors. If I define the skeleton in a header and the variables in a .cpp file, they are not seen in the different files! please help, it's driving me nuts!

I have tested protecting my #includes with #pragma once and with my own #defines (#ifndef SOMETHING, #define, #endif etc...) and I still get errors - if you could write a very small bit of code to show me how it should be done, or something I can at least test to see if it is something beyond my control I would be grateful.

If you put the namespaces in your header files, right click on them in file view, select settings/general and tick the box 'exclude from build'. Then include them in the files they are required using the include directive as you would standard library header files and everything should link.

There is probably an easier way than this but I've tried it and it works and I can't seem to prevent re-definition any other way -

1) Create a static library in MSVC.

2) In the header in the static library put your namespace and variable declaration -

namespace test
{
extern int a;
}

3) In the implementation file of your library, initialise your variable to whatvever value you require (don't forget to include the header file) -

int test::a=10;

4) In your project include the library header file where it's needed and in project settings link to the file created when your library was compiled (I just put the header and library in the standard include/lib paths).

Sorry, just use a header file in your project with the namespace and extern'd variable and another implementation file that includes this header and defines the variable, then include the header where it's needed

The storage is defined in the second file (namespace imp in my example). Include both these files as part of your project, but only include the namespace header in the files that the namespace is needed.