Search code examples
c++linkerexternlinkage

Can I extern the entire namespace?


Is there a method to declare a namespace with the extern modifier so that the namespace's entire content is externally linked?


Solution

  • You seem to be asking two questions. One regarding external linkage and one regarding the extern specifier.


    Regarding the linkage, there is not really a need for such a syntax.

    External linkage is already the default at namespace scope, whether in the global namespace or another namespace, except for const non-template non-inline variables and members of anonymous unions. (https://en.cppreference.com/w/cpp/language/storage_duration#internal_linkage)

    So the other way around, syntax to make all names in a namespace have internal linkage, is more likely to be required and this is possible with unnamed namespaces

    namespace {
        //...
    }
    

    in which all declared names have internal linkage.


    Regarding the extern specifier used to turn a variable definition into just a declaration at namespace scope or to give it external linkage explicitly, I am not aware of any syntax to apply it to a whole namespace. You will have to specify it explicitly on each variable.