0

I'm converting an old C/C++ utility library to managed IJW DLL. Trying to understand link error LNK2020. I have a C/C++ static library now being compiled as a DLL with /CLR and a console app with the utility lib DLL as reference. I get unresolved token on all of my C functions in the DLL. I don't get link error for the one ref class I have in the library DLL.

Error   1   error LNK2020: unresolved token (0A00000B) 
"void __clrcall BwLibM::Reallocx(void * *,unsigned int,class System::String ^,unsigned int)" (
?Reallocx@BwLibM@@$$FYMXPAPAXIP$AAVString@System@@I@Z)  C:\ICL5K\ICL5K\OPENSOUR.obj ICL5K

When I use dumpbin /exports on the utility DLL assembly, I get no exported symbols.

Microsoft (R) COFF/PE Dumper Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file bwlibm.dll
File Type: DLL
  Summary
        4000 .data
       26000 .rdata
        1000 .reloc
        3000 .rsrc
        F000 .text

When I use dumpbin /relocations I do see the undefined function and the prototype exactly matches the LNK2020 error message.

FE5  HIGHLOW 100365E4  __mep@
?Reallocx@BwLibM@@$$FYMXPAPAXIP$AAVString@System@@I@Z ([MEP] 
 void __clrcall BwLibM::Reallocx(void * *,unsigned int,class System::String ^,unsigned int))

Should I be seeing the function in the dumpbin /export output? I tried exporting the functions using classic DLL import/export technique with __declspec(dllexport) and __declspec(dllimport) but got the error

Error 1 error C3395: 'BwLibM::Reallocx' : __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention C:\ICL5K\BwLibM\REALLOCX.CPP 34 1 BwLibM

Source to one of the utility functions looks like this: Reallocx.cpp

namespace BwLibM {
    void Reallocx(void **ptr, size_t nBytesI, System::String^ file, unsigned line)
    {
     . . .
    }
} // namespace BwLibM

===BwLibM.h==

namespace BwLibM {
 . . .
void Reallocx(void **ptr, size_t nBytesI, System::String^ file, unsigned line);
 . . .
}

I posted a similar issue a few months ago where the solution was to set character set on library and application to Multi-Byte. In this case DLL and Console App are set to Multi-Byte Character Set and linker error persists.

Cœur
  • 37,241
  • 25
  • 195
  • 267
JonN
  • 2,498
  • 5
  • 33
  • 49
  • 1
    You are getting the linker error because the function isn't exported. You can't export the function because it is compiled with /clr in effect. Just slapping on /clr on legacy code isn't going to work, you need to use .NET conventions. Which dictates that managed code is wrapped in a ref class. – Hans Passant Sep 11 '12 at 17:40
  • That's the direction I headed in after posting this. I am now wrapping the C functions in a ref class as static methods. Is that the most straightforward way to accomplish this? Am I correct in my understanding now that it isn't possible to have standalone C/C++ functions in managed environment. All methods in managed code must belong to a class. Is that correct? – JonN Sep 11 '12 at 18:37
  • When I included one of these utility function's source file directly in my Console app project it linked just fine. Was that because the function was actually compiled as unmanaged but callable as such from within the console app's routines that were also stand-alone functions? That is are all C/C++ functions automatically compiled as unmanaged even when /CLR is used? – JonN Sep 11 '12 at 18:46

0 Answers0