472,096 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

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

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.
Nov 16 '05 #1
4 11137
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.

"Aaron Queenan" <aq*********************@contingent.com.au> wrote in message
news:OW*************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #2
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 thefollowing error message:

Linking...
LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReportLINK : 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) _CxxThrowExceptionLINK : 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.__ctorLINK : error LNK2020: unresolved token (0A0000A6) exception.__dtorLINK : error LNK2020: unresolved token (0A0000A7) exception.__ctorLINK : error LNK2020: unresolved token (0A0000A8) exception.__ctorLINK : fatal error LNK1120: 16 unresolved externals

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

What should I do to get this to build correctly?

Thanks,
Aaron Queenan.
.

Nov 16 '05 #3
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...b;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

"Tom Reynoldson" <to***************************@complinet.com> wrote in message news:<49****************************@phx.gbl>... 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

---

Nov 16 '05 #4
I was having the same problem. Following links helped me resolve it.

http://msdn.microsoft.com/library/de...ingProblem.asp
http://support.microsoft.com/?id=814472


ed***********@mapinfo.com (Eddie Chaplin) wrote in message news:<24**************************@posting.google. com>...
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...b;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

"Tom Reynoldson" <to***************************@complinet.com> wrote in message news:<49****************************@phx.gbl>...
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

---

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jesse McGrew | last post: by
12 posts views Thread by Markus Ewald | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.