Trying to mix managed and unmanaged code

G

Guest

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
 
M

MPadovani

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.
 

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