Microsoft KB Archive/248883

= You receive a LNK2001 error when you link Visual C++ 5.0 and Visual C++ 6.0 binaries =

Article ID: 248883

Article Last Modified on 1/9/2006

-

APPLIES TO


 * Microsoft Visual C++ 6.0 Service Pack 5
 * Microsoft Visual C++ 5.0 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-



This article was previously published under Q248883



Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.



SUMMARY
When you try to build an executable (.exe) file in Visual C++ 6.0 that references a constant array variable that is defined in a Visual C++ 5.0 dynamic link library (DLL) or a Visual C++ 5.0 static library, you receive the following link error:

error LNK2001: unresolved external symbol

This also occurs under the following circumstances:
 * You try to build an .exe file in Visual C++ 5.0 with a Visual C++ 6.0 DLL or a Visual C++ 6.0 static library.
 * You try to build an .exe file in Visual C++ .NET with a Visual C++ 5.0 DLL or a Visual C++ 5.0 static library.
 * You try to build an .exe file in Visual C++ 5.0 with a Visual C++ .NET DLL.



CAUSE
The Visual C++ 5.0 and Visual C++ 6.0 compilers generate decorated names for a constant array symbol differently. This is because the compilers treat the constant qualifier in the declaration of the array variable differently. For example, in the following code: extern const long test[2] = { 1,2 }; the array variable that is named test in Visual C++ 5.0 is a pointer to a constant array. However, in Visual C++ 6.0, the same array variable is a constant pointer to a constant array.



WORKAROUND
When you generate a Visual C++ 5.0 DLL or static library that exports a constant array variable (to be referenced by a Visual C++ 6.0 .exe file), you can add the following EXPORTS entry to the module definition (DEF) file to generate the DLL or static library. This DEF file maps the internal symbol "?test@@3PBJB" to an external symbol '"?test@@3QBJB" as follows: EXPORTS ?test@@3QBJB=?test@@3PBJB Similarly, when you export a constant array variable from a Visual C++ 6.0 DLL or Visual C++ 6.0 static library (to be referenced in a Visual C++ 5.0 .exe file), you can add the following EXPORTS entry to the DEF file to generate the DLL or static library: EXPORTS ?test@@3PBJB=?test@@3QBJB To use the DEF file to generate the DLL, follow these steps:

Note Assume that the module definitions are stored in a file that is named library.def.   Assume that the code for the DLL is library.cpp. At the command prompt type the following command to create an object library:

cl /c library.cpp   At the command prompt type the following command to create a dynamic link library:

link /dll /def:library.def library.obj

where library.obj is the object file that is generated in Step 1. 



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Open Notepad. On the File menu, click New.</li>  Paste the following code: __declspec(dllexport) extern const long test[2] = { 1,2 }; </li> Click Save. In the Save dialog box, type library.cpp as the name of the file</li> To create a DLL, follow these steps:   At the command prompt type the following command to create an object file from the .cpp file: <pre class="fixed_text">cl /c library.cpp The object file that is created is library.obj. </li>  At the command prompt, type the following command to create a DLL from the object file that is created in Step a: link /dll library.obj </li></ol> </li>  At the command prompt, type the following command: <pre class="fixed_text">dumpbin /exports library.dll where library.dll is the DLL that is generated in Step b.

In the output of the dumpbin command notice the "?test@@3PBJB" decorated name that is generated for the constant array by the compiler. </li> Follow the steps in the "Workaround" section of this article to create a DLL in Visual C++ 6.0. The decorated name that is generated for the constant array variable that is named test is '"test@@3QBJB".</li></ol>

The LNK2001 error that is described in the Symptoms section occurs because the compilers for the constant array variable for Visual C++ 6.0, and for Visual C++ 5.0 generate different decorated names. The error occurs when you try to link the following


 * A constant array reference in a Visual C++ 6.0 .exe, to a constant array definition in a Visual C++ 5.0 DLL or Visual C++ 5.0 static library
 * A constant array reference in a Visual C++ 5.0 .exe to the constant array definition in a Visual C++ 6.0 DLL or Visual C++ 6.0 static library
 * A constant array reference in a Visual C++ .NET .exe to a constant array definition in a Visual C++ 5.0 DLL or Visual C++ 5.0 static library. (Decorated names that are generated by the Visual C++ .NET compiler for constant array variables are the same as those of the Visual C++ 6.0 compiler.)

<div class="references_section">