I cannot reference an external variable from the namespace using extern . It works from a global scope, but as soon as the namespace is thrown there, it does not bind.
My const file looks like this:
StringConstants.cpp
#include "MyString.h" MyString test1("string1"); MyString test2("string2");
The main program is as follows:
main.cpp
#include <stdio.h> #include "MyString.h" extern MyString test1; namespace { extern MyString test2; } int main(void) { printf("%s\n", test1.Str()); printf("%s\n", test2.Str()); }
I get similar errors in both GCC and Visual Studio:
gcc main.o StringConstants.o -o main main.o:main.cpp:(.text+0x49): undefined reference to `(anonymous namespace)::test2' collect2: ld returned 1 exit status 1>Linking... 1>main.obj : error LNK2001: unresolved external symbol "class MyString `anonymous namespace'::test2" ( ?test2@ ?A0x0df4aa01@ @ 3VMyString@ @A) 1>C:\p4\namespace_repro\namespace_repro2\Debug\namespace_repro2.exe : fatal error LNK1120: 1 unresolved externals
I tried qualifying the reference to test2 ( extern MyString ::test2 ), but it just thinks test2 is a static member of MyString. A named namespace does not behave differently than anonymous. For various reasons, we donβt want to remove namespaces or put external elements outside of namespaces.
Here are other files for completeness:
MyString.h
class MyString { public: MyString(const char* str): mStr(str) {}; const char* Str() const { return mStr; } private: const char* mStr; };
Makefile
CC=gcc CFLAGS=-Wall main: StringConstants.o main.o
The goals of this system are that constants are all defined in one file and that they are solved during communication, and not in the header. It seemed that the above code would work, but since it was rejected by two different linkers, it seems that my understanding of C ++ is not enough. Advice on how to make it work, besides putting external external spaces?