470,631 Members | 2,027 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

Trying to mix managed and unmanaged code

The project that I am on is going to require me to mix managed and unmanaged
code. I created a vc++ class library (.net) project bane TestLib.

I inserted the following test code in testLib.h. The code came from the MS
example on mixing managed and unmanaged code. When I build the project, I get
two unresolved external symbols on from the MClass Constructor and
Destructor. I'll guess I am missing a build switch. The doc says I need /clr
and /LD but they have no affect. The actual link errors follow the code.

Thanks in advance...Chuck

// TestLib.h
#pragma once
using namespace System;

namespace TestLib
{
class CppClass
{
public:
// constructor
CppClass() {}
// destructor
~CppClass() {}
// methods
void native_f() {}
};

public __gc class MClass
{
public:
// constructor
MClass() { m_pC = new CppClass(); }
// destructor
~MClass() { delete m_pC; }
// method
void managed_f() { m_pC->native_f(); }
private:
CppClass * m_pC;
};

}

Compiling...
TestLib.cpp
Linking...
TestLib.obj : error LNK2001: unresolved external symbol "void * __cdecl
operator new(unsigned int)" (??2@$$FYAPAXI@Z)
TestLib.obj : error LNK2001: unresolved external symbol "void __cdecl
operator delete(void *)" (??3@$$FYAXPAX@Z)
D:\Source Files\statrad\ScannerTest\Debug\TestLib.dll : fatal error LNK1120:
2 unresolved externals
Nov 17 '05 #1
2 1645
Hi.

add msvcrt.lib (or msvcrtd.lib) to the project dependencies (Project
properties/Linker/Input/Additional Dependencies)

see also: http://support.microsoft.com/?id=814472

hope this helps.

Csaba

Nov 17 '05 #2
Hi,

I've done similar projects and found that I had to play with other
settings also to get rid of every problem. Here are the VS 2003
solution settings that ultimately worked for me.

General:
Use of MFC: Use MFC in a Shared DLL

Compiler:
Runtime Lib: Multi-threaded DLL (/MD)
Compile As Managed: Yes for managed files, No for unmanaged

Linker:
Additional Dependencies: mscoree.lib msvcrt.lib
Ignore all default libs: No
Ignore specific libs: nafxcw.lib;libcmt.lib;libc.lib nochkclr.obj
Force symbol reference: __DllMainCRTStartup@12
Additional options: /noentry

Then add the ManagedWrapper.cpp file as per the MSDN article 814472
(and call the minitialize() and mterminate() functions from within your
VB.NET,c#, etc. code before and after using the new managed/unmanaged
assembly.
ctrucza wrote:
Hi.

add msvcrt.lib (or msvcrtd.lib) to the project dependencies (Project
properties/Linker/Input/Additional Dependencies)

see also: http://support.microsoft.com/?id=814472

hope this helps.

Csaba


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Eric Twietmeyer | last post: by
4 posts views Thread by repstat | last post: by
2 posts views Thread by Sandy | last post: by
6 posts views Thread by nicolas.hilaire | last post: by
9 posts views Thread by Amit Dedhia | last post: by
25 posts views Thread by Koliber (js) | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.