xmllmx
2010-11-15 14:09:19 UTC
Several lines of code are woth a thousand words:
I have three simple files: header.h, main.cpp, other.cpp
==== CODE BEGIN ====
// header.h
#pragma once
const void* p = 0;
// main.cpp
#include "header.h"
int main()
{
return 0;
}
// other.cpp
#include "header.h"
==== CODE END ====
When compiling the simplest project, the VC++ 2010 complains as
follows:
ClCompile:
other.cpp
main.cpp
Generating Code...
other.obj : error LNK2005: "void const * const p" (?p@@3PBXB) already
defined in main.obj
D:\Test\Debug\bug.exe : fatal error LNK1169: one or more multiply
defined symbols found
Build FAILED.
Time Elapsed 00:00:00.29
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
I am sure this is a bug of VC++ 2010, because of the following two
references:
#1, The C++ standard says: (at page 140 of n3126)
"Objects declared const and not explicitly declared extern have
internal linkage."
#2, The MSDN says: (at: http://msdn.microsoft.com/en-us/library/357syhfh(VS.80).aspx)
"In C, constant values default to external linkage, so they can
appear only in source files. In C++, constant values default to
internal linkage, which allows them to appear in header files.
The const keyword can also be used in pointer declarations."
I have three simple files: header.h, main.cpp, other.cpp
==== CODE BEGIN ====
// header.h
#pragma once
const void* p = 0;
// main.cpp
#include "header.h"
int main()
{
return 0;
}
// other.cpp
#include "header.h"
==== CODE END ====
When compiling the simplest project, the VC++ 2010 complains as
follows:
ClCompile:
other.cpp
main.cpp
Generating Code...
other.obj : error LNK2005: "void const * const p" (?p@@3PBXB) already
defined in main.obj
D:\Test\Debug\bug.exe : fatal error LNK1169: one or more multiply
defined symbols found
Build FAILED.
Time Elapsed 00:00:00.29
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
I am sure this is a bug of VC++ 2010, because of the following two
references:
#1, The C++ standard says: (at page 140 of n3126)
"Objects declared const and not explicitly declared extern have
internal linkage."
#2, The MSDN says: (at: http://msdn.microsoft.com/en-us/library/357syhfh(VS.80).aspx)
"In C, constant values default to external linkage, so they can
appear only in source files. In C++, constant values default to
internal linkage, which allows them to appear in header files.
The const keyword can also be used in pointer declarations."