Linker Error Anonymous Namespace
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Undefined reference linker error when implementing function declared in anonymous namespace up vote 1 down vote favorite When defining a function signature in an anonymous namespace within a .hpp, is it valid to place the implementation of that function within the .cpp? When I do so I get an undefined reference error. Example: //hpp #ifndef __BAR_HPP_ #define __BAR_HPP_ namespace foo { namespace { struct Bar { void func(); }; } } #endif //cpp using foo; void Bar::func() { //... } c++ namespaces share|improve this question asked May 3 '11 at 8:16 Graeme 1,7952054 2 Why would you use an anonymous namespace like that? And names like __BAR_HPP_ which contain a double underscore, or names that begin with an underscore and an uppercase letter are reserved for the C++ implementation. –nbt May 3 '11 at 8:19 I used it like this because the function func() is only pertinent to the Bar class. –Graeme May 3 '11 at 8:20 Well, make it a private function of Bar. Or put it in an anonymous namespace in the Bar implementation file. –nbt May 3 '11 at 8:28 Oddly, this seems to work for me... although I do need to change 'using foo' to 'using namespace foo'... –forsvarir May 3 '11 at 8:29 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted Think of this: namespace foo { struct Bar { void func(); }; } void Bar::func() { /*impl...*/ } Your code doesn't work for the same reason this code doesn't -- the definition is being provided in the wrong scope. What's needed is: void foo::Bar::func() { /*impl...*/ } But what do you put in place of foo:: to refer to the name of an anonymous namespace? It doesn't have one. Bottom line: it's not possible to declare something inside of an anonymous namespace then define it elsewhere, as no mechanism exists for specifying the proper scope. share|improve this answer answered May 3 '11 at 8:24 ildjarn 48k579138 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Namespace function linker error when used in multiple classes up http://stackoverflow.com/questions/5866856/undefined-reference-linker-error-when-implementing-function-declared-in-anonymou vote 0 down vote favorite I am simply writing a function that I want to use in my code for debugging WITHOUT throwing exceptions. I want a defined variable DEBUG_MODE that when set to true causes the function to print a given message and when set to false prints nothing. I want it available to any class that imports the file without having to create a class, i.e. I http://stackoverflow.com/questions/23232265/namespace-function-linker-error-when-used-in-multiple-classes include "Debug.h" and can call Debug::Log(string message). I read about the advantages of using namespace functions so I wrote it as such but perhaps I don't understand the proper usage of namespace functions. When I include my header file with the namespace function in one class it works great, when I use it in two classes in two different files I get a linker error: Error 1 error LNK2005: "void __cdecl Debug::Log(char const *)" (?Log@Debug@@YAXPBD0@Z) already defined in Foo.obj... Here is the code for my namespace functions: #pragma once #include
— demoFramework — adlFramework — adl+tagFramework — specMach7 benchmarkNo double-to-intOverloading — enable_ifOverloading — enable_if(2)Overloading — enable_if(3)Overloading — TagdispatchParsing xmlVariant with Mach7Variant with visitor ← Clever overloading Inadvertent conversions → Inline functions Posted on July 14, 2014 by Andrzej KrzemieĊski Inlining functions can improve or worsen your program's performance (however you define ‘performance'). It https://akrzemi1.wordpress.com/2014/07/14/inline-functions/ has been described in detail in Herb Sutter's GotW #33. Compiler can decide to inline your function, even if it was not declared inline and conversely: it can decide not to inline it even if it http://www.cplusplus.com/forum/general/39196/ is declared inline. So, you might be tempted to think that declaring functions as inline has no useful portable meaning in C++. This is not so. I have found inline functions useful, and its usefulness has nothing linker error to do with inlining. In order to be able to inline a function, compiler has to have its definition available at any time (at least this is the case for compilers that do not perform heavy link-time optimizations), in any compiled source file (or translation unit, if you will). In order to provide that, for inline functions it is allowed and in fact required that their definition (their body) is defined in every translation linker error anonymous unit in which the function is used. Contrast this with normal (non-inline) functions: if you define your function twice in two source files, you will get a linker error: a function can be defined only once. But not inline functions: their definitions can be duplicated. Compiler is not allowed to complain about multiple definitions. Duplicated definitions, however, must define exactly same function. Not only should they be identical token by token, but the same name of a variable must refer to the very same object in each source file. For instance, the following is likely an invalid definition of an inline function: // content of header file "file.h" namespace { int i = 0; } inline int get_i() { return ++i; } If you include the above header from more than one source file, although the function body will look identical in each file, each function will refer to a different variable i. This will render an ill-formed program, but compilers are not required to diagnose this. Back to the key sentence above, "compiler is not allowed to complain about multiple definitions" -- this is the feature of inline functions that I find useful. First usage of this property that I ever encountered was when I wanted to change a function template into an ordinary function. This is
keyword. I do not understand what an unnamed namespace gives you that cannot just as easily be achieved using the static keyword. One example, which I found at http://stackoverflow.com/questions/4422507/superiority-of-unnamed-namespace-over-static, is this: 1
2
3
4
namespace { class sample_class { /* class body */ }; } This cannot be achieved by using the static keyword. While it is certainly true that the static keyword cannot be used in this case, I fail to see what you achieve by putting sample_class in an unnamed namespace. If you write a class definition in a .cpp file, there is (I believe) no way to access it from another .cpp file, regardless of whether it is placed in an unnamed namespace or not. So what benefit does the unnamed namespace actually give? -- Claus Mar 23, 2011 at 9:10am UTC Bazzy (6281) If a definition is not inside an unnamed namespace the linker will try to link it to other symbols with the same name even if those are in other source files Mar 23, 2011 at 10:04am UTC kbw (7986) ... or more generally, static applies to instances whereas types and instances can be placed in namespaces. Mar 23, 2011 at 10:44am UTC oz1cz (7) I do not see the linker trying to link the names of classes the way you describe. Consider these two pieces of code: File 1: 1
2
3
4
5
6
7
8
9
class Alpha { public: int i; }; int main() { Alpha a; } File 2: 1
2
3
4
5
6
7
8
void Alpha() { } void xx() { Alpha(); } Although the symbol "Alpha" is defined in both files, I have no problem compiling and linking these files together. (I've tried both with GNU's and Microsoft's compiler.) Yes, if Alpha is a function or a variable in both files, the linker will complain, but that problem can be solved with "static". So I still want to know: What does an unnamed namespace give me? Can anybody show me a piece of code that will not work without unnamed namespaces and which cannot be solved equally well using "static"? Mar 23, 2011 at 10:48am UTC Bazzy (6281) 1
2
3
4
5
6
7
8
// file 1 namespace S { void foo() { // something } } 1
2
3
4
5
6
7
8
9
10
// file 2 class S { void foo(); }; void S::foo() { // something else } Result: /tmp/ccvDdNNj.o: In function `S::foo()': /home/bazzy/file2.cpp:7: multiple definition of `S::foo()' /tmp/ccrSkKSi.o:/home/bazzy/file1.cpp:4: first defined here collect2: ld returned 1