Microsoft KB Archive/129210

= FIX: Linker Errors on Array of Constant Pointers to Constants =

Article ID: 129210

Article Last Modified on 10/17/2003

-

APPLIES TO


 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.51
 * Microsoft Visual C++ 1.52 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition

-



This article was previously published under Q129210



SYMPTOMS
Building a project that includes the code listed in the "Sample Code" section of this article results in the following link error or warning:

For Visual C++ for Windows:

   MODULE2.OBJ(module2.cpp) : error L2025: char const __near* __near* __near str : symbol defined more than once LINK returned error code 2.

For Visual C++, 32-bit Edition:

   module2.obj: warning LNK4006: xxxxx already defined in         module1.obj; second definition ignored.

Here xxxxx is the decorated name for str.



RESOLUTION
To work around the problem, explicitly declare the const as static, as in this example: static const char *const str[2] = { "string1", "string2" }; This definition can be placed in a header file, and can be included in multiple source files. However, there are implications for the size of the program when you use this workaround. That is, a copy of the array and of the strings will be in every module that includes the header file, whether you use it or not. The sample code below shares this problem.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++, 32- bit Edition, version 2.0.



MORE INFORMATION
In C++, const objects do not have external linkage, unless they are explicitly declared extern. Const objects have internal linkage, meaning that the name of the object is local to the source file in which it is declared.

The following sample code has two source files and one header file. The array of const pointers to const chars is included twice in the program. However, because this is C++, const objects should have internal linkage, the arrays should only be visible in the source file they are declared in. This means that the linker should not fail with an L2025 error, or report the LNK4006 warning.

Sample Code
// Compile options needed: None

// Test.h

const char *const str[] = { "string1", "string2" }; // error

// Module1.cpp


 * 1) include "test.h"          // included test.h once in project.
 * 2) include 

void function1 {  cout << "In Function1" << endl; }

// Module2.cpp


 * 1) include "test.h"         // included test.h twice in project
 * 2) include 

extern void function1;

void function2 { cout << "In Function2" << endl; }

void main { function1; function2;

return; }

Additional query words: 1.00 1.50 1.51 1.52 8.00 8.00c

Keywords: kbbug kbfix kbcodegen kbcpponly kbcompiler KB129210

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.