Microsoft KB Archive/822329

= LNK1254, LNK1284, and LNK1235 linker errors may occur while compiling a C source file with the /clr compiler option =

Article ID: 822329

Article Last Modified on 1/17/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





SYMPTOMS
When you compile a C source file (unmanaged source) with other managed source files that refer to symbols that are defined in the unmanaged source, and you specify the /clr compiler switch, you receive the following linker error message in Visual C++ .NET 2002:

LINK : fatal error LINK1254: metadata for symbol  inconsistent with COFF symbol table

You receive the following linker errors in Visual C++ .NET 2003:







CAUSE
The earlier versions of Visual C++ may not support the C or C++ source files with the /clr switch because earlier versions of Visual C++ do not have managed code or the /clr switch.



RESOLUTION
Compile the unmanaged source (including the C source files) into an object file. Similarly, compile files that constitute the managed files of the project into another object file. Link the object files that are generated from the managed and unmanaged source files into a single executable by using the linker. The following steps assume that you have unmanaged source in UnManaged.c and that you have managed source in Managed.cpp. To build your project, follow these steps:   Compile UnManaged.c with the following command: cl /c UnManaged.c   Compile Managed.cpp with the following command: cl /clr /c Managed.cpp   Link the object files that are generated in steps 1 and 2 with the following command: link /NODEFAULTLIB:LIBC Managed.obj UnManaged.obj 



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
  Paste the following code in Notepad, and then save the file as UnManaged.h :
 * 1) include <stdio.h>

// Both of these generate LNK1254 in 2002

//Generates LNK1235 - In 2003 //int willNotLink(unsigned char data[20]); //int willLink(unsigned char *data);

//Generates LNK1284 - In 2003 int willLink(unsigned char data[20]); int willNotLink(unsigned char *data); </li>  Paste the following code in Notepad, and then save the file as UnManaged.c : //defines two functions to be referred in a C++ file.
 * 1) include &quot;UnManaged.h&quot;

int willLink(unsigned char *data) {   printf(&quot;This is the function named willLink\n&quot;); return 0; }

int willNotLink(unsigned char data[20]) {   printf(&quot;This is the function named willNotLink\n&quot;); return 0; } </li>  Paste the following code in Notepad, and then save the file as Managed.cpp : // This is the main file.


 * 1) pragma once

extern &quot;C&quot; { }
 * 1) include &quot;UnManaged.h&quot;


 * 1) using <mscorlib.dll>

using namespace System;

namespace LinkerProblem {   public __gc class Class1 {   public: void test {           unsigned char data[20]; willLink(data);

willNotLink(data); }   }; };

void main {   LinkerProblem::Class1 *myClass = new LinkerProblem::Class1; myClass->test; } </li>  At the Visual C++ .NET command prompt, type the following command: cl /clr Managed.cpp UnManaged.c </li> Notice that you receive the linker problem that is described in the &quot;Symptoms&quot; section. In Visual C++ .NET 2002, you receive the LNK1254 linker error, and in Visual C++ .NET 2003 you receive the LNK1284 linker error.</li>  To see the LNK1235 linker error in Visual C++ .NET 2003, modify the contents of the UnManaged.h header file. To do this, uncomment the two declarations under the following comment: //Generates LNK1235 - In 2003 and then comment the two declarations under the following comment: //Generates LNK1284 - In 2003 </li> Compile the code by running the command that is specified in step 4 at the Visual Studio .NET 2003 command prompt. You receive the LNK1235 linker error in Visual C++ .NET 2003.</li></ol>

Keywords: kbappdev kbprb kbcompiler KB822329

-

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

© Microsoft Corporation. All rights reserved.