Re: Anonymous namespace and internal linkage
On Feb 6, 9:33 am, Anand Hariharan <mailto.anand.hariha...@gmail.com>
wrote:
This code fails during linking:
namespace {
void FuncA()
{
/*extern*/ void FuncB(void);
FuncB();
}
void FuncB() {}
}
int main()
{
FuncA();
}
Is it to be expected?
Isn't 'extern' qualifier meaningless here?
It should be.
It turns out that the linker does not have a problem when the
'extern' is uncommented.
That sounds like an error.
Technically, of course, you have undefined behavior (both ways).
But the FuncB called in FuncA is a function in the anonymous
namespace; the FuncB you define isn't, so it isn't the same
function.
(FWIW: both Sun CC and g++ give a link error in both cases, with
or without the extern. So does the version of VC++ that I have
here: the one from Visual Studios 8.)
FWIW, here is the diagnostic, copy-pasted:
--- BEGIN ---
C:\temp>cl foo.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
foo.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:foo.exe
foo.obj
foo.obj : error LNK2019: unresolved external symbol "void __cdecl FuncB
(void)" (
?FuncB@@YAXXZ) referenced in function "void __cdecl `anonymous
namespace'::FuncA
(void)" (?FuncA@?A0xd21949b8@@YAXXZ)
foo.exe : fatal error LNK1120: 1 unresolved externals
--- END ---
That's interesting. When I try this with VC++, I get:
952@~/tmp/news (21): cl -Dextern= $VCPPFLAGS anon.cc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
14.00.50727.762 for 80x86
...
anon.obj : error LNK2019: unresolved external symbol "void __cdecl
`anonymous na
mespace'::funcB(void)" (?funcB@?A0xb676e5d1@@YAXXZ) referenced in
function "void
__cdecl `anonymous namespace'::funcA(void)" (?funcA@?
A0xb676e5d1@@YAXXZ)
(I've just cited the relevant lines: the version and the error
message.) And this whether or not the "extern" is present.
Note the difference in the error message, however---it clearly
says that the missing function is in the anonymous namespace.
The error messages of g++ and Sun CC are equally clear in this
respect (for g++, at least under Linux---under Solaris, the Sun
linker is used, and it doesn't know how to demangle the g++
names, so the anonymous bit must be deduced by recognizing the
mangling pattern).
Since the version of the compiler you're using seems more
recent than mine, I'd say that there is a regression error if
it accepts the code, either with or without the "extern"; you
should probably report it.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]