LNK1120 when compiling a C++ library to .NET managed code

A

Aaron Queenan

When I build a C++ library to .NET using the managed C++ compiler, I get the
following error message:

Linking...
LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport
LINK : error LNK2020: unresolved token (0A000007) memset
LINK : error LNK2020: unresolved token (0A000008) free
LINK : error LNK2020: unresolved token (0A00000A) atexit
LINK : error LNK2020: unresolved token (0A000028) wcscpy
LINK : error LNK2020: unresolved token (0A00002D) _CxxThrowException
LINK : error LNK2020: unresolved token (0A000031) delete
LINK : error LNK2020: unresolved token (0A00003C) delete[]
LINK : error LNK2020: unresolved token (0A000045) malloc
LINK : error LNK2020: unresolved token (0A000049) memmove
LINK : error LNK2020: unresolved token (0A000054) memcpy
LINK : error LNK2020: unresolved token (0A000060) strlen
LINK : error LNK2020: unresolved token (0A000067) bad_cast.__ctor
LINK : error LNK2020: unresolved token (0A0000A6) exception.__dtor
LINK : error LNK2020: unresolved token (0A0000A7) exception.__ctor
LINK : error LNK2020: unresolved token (0A0000A8) exception.__ctor
LINK : fatal error LNK1120: 16 unresolved externals

Note that "Ignore All Default Libraries" is set to "No" and that the code
builds fine when using the unmanaged C++ compiler and targetting native
code. If I use the DLL version of the library, I get a slightly different
list of errors.

What should I do to get this to build correctly?

Thanks,
Aaron Queenan.
 
A

Aaron Queenan

I've just added MSVCRTD.LIB MSVCPRTD.LIB to the list of libraries, but I now
get the following errors:

Field.obj : error LNK2001: unresolved external symbol "char * __cdecl
strdup(char const *)" (?strdup@@$$J0YAPADPBD@Z)
Platform.obj : error LNK2001: unresolved external symbol "int __cdecl
putenv(char const *)" (?putenv@@$$J0YAHPBD@Z)
Platform.obj : error LNK2001: unresolved external symbol "char * __cdecl
strupr(char *)" (?strupr@@$$J0YAPADPAD@Z)

These are pretty wierd, since they have C++ symbol names even though they
are C functions. Even putting :: in front of the functions where called
doesn't fix the problem.

If I use the static MT libraries the problem goes away, but of course things
like putenv() won't work across two instances of the static libraries, so it
isn't really what I want to do.

What else can I try?

Thanks,
Aaron Queenan.
 
T

Tom Reynoldson

I'm getting exactly the same problem.

It appears to occur only when I include standard library
components. The errors differ very slightly depending on
which standard #include files I use but are always a list
of LNK2020 unresolved tokens followed by a LNK1120.

I've tried putting #pragma unmanaged/managed statements in
a variety of different positions around both the standard
#include <> 's, the function prototypes and the function
bodies all to no avail.
---
Examples:
---
#include "stdafx.h"
#include <string>
#pragma unmanaged
class stltest
{
public:
int a;
int b;
std::string s;
};
#pragma managed

produces:
------ Build started: Project: mtest1, Configuration:
Debug Win32 ------

Compiling...
Stdafx.cpp
Compiling...
stltest.cpp
mtest1.cpp
AssemblyInfo.cpp
Generating Code...
Compiling resources...
Linking...
LINK : error LNK2020: unresolved token (0A000013)
_CxxThrowException
LINK : error LNK2020: unresolved token (0A000023) delete
LINK : fatal error LNK1120: 2 unresolved externals

---
Whereas moving the #pragma's around a little:

#include "stdafx.h"

#pragma unmanaged
#include <string>

class stltest
{
public:
int a;
int b;
std::string s;
};
#pragma managed

produces:

Compiling...
stltest.cpp
mtest1.cpp
Generating Code...
Linking...
mtest1.obj : error LNK2019: unresolved external symbol
_memcpy referenced in function "public: static char *
__cdecl std::char_traits<char>::copy(char *,char const
*,unsigned int)" (?copy@?
$char_traits@D@std@@SAPADPADPBDI@Z)
stltest.obj : error LNK2001: unresolved external symbol
_memcpy
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol _memcpy
mtest1.obj : error LNK2019: unresolved external symbol
_memmove referenced in function "public: static char *
__cdecl std::char_traits<char>::move(char *,char const
*,unsigned int)" (?move@?
$char_traits@D@std@@SAPADPADPBDI@Z)
stltest.obj : error LNK2001: unresolved external symbol
_memmove
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol _memmove
mtest1.obj : error LNK2019: unresolved external
symbol "void __cdecl operator delete(void *)" (??
3@YAXPAX@Z) referenced in function "public: void
__thiscall std::allocator<char>::deallocate(char
*,unsigned int)" (?deallocate@?
$allocator@D@std@@QAEXPADI@Z)
stltest.obj : error LNK2001: unresolved external
symbol "void __cdecl operator delete(void *)" (??
3@YAXPAX@Z)
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol "void __cdecl operator delete(void *)" (??
3@YAXPAX@Z)
libcpmtd.lib(nomemory.obj) : error LNK2001: unresolved
external symbol "void __cdecl operator delete(void *)" (??
3@YAXPAX@Z)
mtest1.obj : error LNK2019: unresolved external symbol
___CxxFrameHandler referenced in function __ehhandler$?
_Copy@?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@IAEXII@Z
stltest.obj : error LNK2019: unresolved external symbol
___CxxFrameHandler referenced in function $L12690
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol ___CxxFrameHandler
mtest1.obj : error LNK2019: unresolved external symbol
__except_list referenced in function "protected: void
__thiscall std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >::_Copy
(unsigned int,unsigned int)" (?_Copy@?$basic_string@DU?
$char_traits@D@std@@V?$allocator@D@2@@std@@IAEXII@Z)
stltest.obj : error LNK2001: unresolved external symbol
__except_list
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol __except_list
mtest1.obj : error LNK2019: unresolved external symbol
__CxxThrowException@8 referenced in function $L12724
stltest.obj : error LNK2001: unresolved external symbol
__CxxThrowException@8
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol __CxxThrowException@8
libcpmtd.lib(nomemory.obj) : error LNK2001: unresolved
external symbol __CxxThrowException@8
mtest1.obj : error LNK2019: unresolved external symbol
_free referenced in function "void __cdecl
std::_DebugHeapDelete<void>(void *)" (??
$_DebugHeapDelete@X@std@@YAXPAX@Z)
stltest.obj : error LNK2001: unresolved external symbol
_free
libcpmtd.lib(xlock.obj) : error LNK2019: unresolved
external symbol _atexit referenced in function _$E1
libcpmtd.lib(nomemory.obj) : error LNK2001: unresolved
external symbol _atexit
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol "public: __thiscall exception::exception
(class exception const &)" (??0exception@@QAE@ABV0@@Z)
libcpmtd.lib(nomemory.obj) : error LNK2001: unresolved
external symbol "public: __thiscall exception::exception
(class exception const &)" (??0exception@@QAE@ABV0@@Z)
libcpmtd.lib(string.obj) : error LNK2001: unresolved
external symbol "const type_info::`vftable'" (??
_7type_info@@6B@)
libcpmtd.lib(nomemory.obj) : error LNK2001: unresolved
external symbol "const type_info::`vftable'" (??
_7type_info@@6B@)
libcpmtd.lib(string.obj) : error LNK2019: unresolved
external symbol "public: virtual __thiscall
exception::~exception(void)" (??1exception@@UAE@XZ)
referenced in function $L12220
libcpmtd.lib(nomemory.obj) : error LNK2001: unresolved
external symbol "public: virtual __thiscall
exception::~exception(void)" (??1exception@@UAE@XZ)
libcpmtd.lib(string.obj) : error LNK2019: unresolved
external symbol "public: __thiscall exception::exception
(void)" (??0exception@@QAE@XZ) referenced in
function "public: __thiscall std::logic_error::logic_error
(class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > const
&)" (??0logic_error@std@@QAE@ABV?$basic_string@DU?
$char_traits@D@std@@V?$allocator@D@2@@1@@Z)
libcpmtd.lib(string.obj) : error LNK2019: unresolved
external symbol _strlen referenced in function "public:
static unsigned int __cdecl std::char_traits<char>::length
(char const *)" (?length@?$char_traits@D@std@@SAIPBD@Z)
libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved
external symbol __malloc_dbg referenced in function "void
* __cdecl operator new(unsigned int,struct
std::_DebugHeapTag_t const &,char *,int)" (??
2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved
external symbol __free_dbg referenced in function "void
__cdecl operator delete(void *,struct std::_DebugHeapTag_t
const &,char *,int)" (??
3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
libcpmtd.lib(newop.obj) : error LNK2019: unresolved
external symbol __callnewh referenced in function "void *
__cdecl operator new(unsigned int)" (??2@YAPAXI@Z)
libcpmtd.lib(newop.obj) : error LNK2019: unresolved
external symbol _malloc referenced in function "void *
__cdecl operator new(unsigned int)" (??2@YAPAXI@Z)
libcpmtd.lib(nomemory.obj) : error LNK2001: unresolved
external symbol "public: virtual char const * __thiscall
exception::what(void)const " (?what@exception@@UBEPBDXZ)
libcpmtd.lib(nomemory.obj) : error LNK2019: unresolved
external symbol "public: __thiscall exception::exception
(char const * const &)" (??0exception@@QAE@ABQBD@Z)
referenced in function "public: __thiscall
std::bad_alloc::bad_alloc(char const *)" (??
0bad_alloc@std@@QAE@PBD@Z)
LINK : error LNK2001: unresolved external symbol
__load_config_used


---

Finally the following compiles quite happily (except for
complaining about a missing 'libc.pdb' file - LNK4099)
#include "stdafx.h"

#pragma unmanaged
//#include <string>

class stltest
{
public:
int a;
int b;
//std::string s;
};
#pragma managed

---

If anyone can shed any light on this I would be most
grateful.

Thanks,

Tom
-----Original Message-----
When I build a C++ library to .NET using the managed C++ compiler, I get the
following error message:

Linking...
LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport
LINK : error LNK2020: unresolved token (0A000007) memset
LINK : error LNK2020: unresolved token (0A000008) free
LINK : error LNK2020: unresolved token (0A00000A) atexit
LINK : error LNK2020: unresolved token (0A000028) wcscpy
LINK : error LNK2020: unresolved token (0A00002D) _CxxThrowException
LINK : error LNK2020: unresolved token (0A000031) delete
LINK : error LNK2020: unresolved token (0A00003C) delete[]
LINK : error LNK2020: unresolved token (0A000045) malloc
LINK : error LNK2020: unresolved token (0A000049) memmove
LINK : error LNK2020: unresolved token (0A000054) memcpy
LINK : error LNK2020: unresolved token (0A000060) strlen
LINK : error LNK2020: unresolved token (0A000067) bad_cast.__ctor
LINK : error LNK2020: unresolved token (0A0000A6) exception.__dtor
LINK : error LNK2020: unresolved token (0A0000A7) exception.__ctor
LINK : error LNK2020: unresolved token (0A0000A8) exception.__ctor
LINK : fatal error LNK1120: 16 unresolved externals

Note that "Ignore All Default Libraries" is set to "No" and that the code
builds fine when using the unmanaged C++ compiler and targetting native
code. If I use the DLL version of the library, I get a slightly different
list of errors.

What should I do to get this to build correctly?

Thanks,
Aaron Queenan.


.
 
E

Eddie Chaplin

Tom/Aaron,
I'm having this problem too and it is driving me crazy.
Here's my code:
#pragma once

#include <vector>
#include <string>
using namespace System;

namespace DllProjectwithstring
{
public __gc class Class1
{
};
}

Not too exciting, I was able to finally resolve these linker errors:
LINK : error LNK2020: unresolved token (0A000013) _CxxThrowException
LINK : error LNK2020: unresolved token (0A000023) delete
LINK : fatal error LNK1120: 2 unresolved externals
with help from this link:
http://support.microsoft.com/default.aspx?scid=kb;en-us;140584
It turned out that I was creating a DLL and needed the /MD runtime
library flag instead of /MT which seems to be default (or /MDd and
/MTd if you're in debug mode).

My problem now is I STILL can't get it to link. Here is the latest and
greatest linker error:
DllProjectwithstring error LNK2020: unresolved token (0A000051)
??_7type_info@@6B@
DllProjectwithstring fatal error LNK1120: 1 unresolved externals

If anyone has any idea as to what causes this error I'd really
appreciate it.

Eddie Chaplin
 
H

herbertp

I'm sure my problem's along the same lines.
I'm building a MC++ assembly which doesn't link in release mode, bu
does in debug. It's driving me bonkers, and I would really like som
help! I tried the links provided in the forum but still no joy. Thes
are the three linker errors I get..

1)
SFloggerNET error LNK2001: unresolved external symbol "public
__thiscall std::bad_cast::bad_cast(class std::bad_cast const &)
(??0bad_cast@std@@QAE@ABV01@@Z)

2)followed by..
SFloggerNET error LNK2001: unresolved external symbol "public: virtua
__thiscall std::bad_cast::~bad_cast(void)" (??1bad_cast@std@@UAE@XZ)

3)followed by..
SFloggerNET error LNK2019: unresolved external symbo
"__declspec(dllimport) const std::bad_cast::`vftable'
(__imp_??_7bad_cast@std@@6B@) referenced in function "clas
_STL::basic_ostream<char,class _STL::char_traits<char> > & __cdec
_STL::_M_put_num(class _STL::basic_ostream<char,clas
_STL::char_traits<char> > &,unsigned long)
(?_M_put_num@_STL@@YAAAV?$basic_ostream@DV?$char_traits@D@_STL@@@1@AAV21@K@Z)

I would really appreciate some suggestions etc. Thank you

herbert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top