how to link unmanaged c++ into .net class library to test with nunit?

R

Ray Tayek

hi, i am teaching a class using visual studio 2003 in the labs at
school and trying to get some pure c++ into a dll so i can sic nunit on
it.
trying to put the code below into a .net class library gets unresolved
externs:


pure error LNK2020: unresolved token (0A000006) _CxxThrowException
pure error LNK2020: unresolved token (0A000015) delete
pure fatal error LNK1120: 2 unresolved externals


i have been away from the m$ world for many years and have no clue
how to resolve this. this has to do with linking in some static
flavour of the crt lib (i think). there are some clues at
http://support.microsoft.com/?kbid=814472, but it looks like a
research project.


what i would like to do is to package the pure c++ code into a pure
c++ dll ideally, but *any* kind of assembly would be fine. and then
put the managed tests that nunit will find into another assembly. not
sure how to expose the public api from c to the managed code.


is anyone at all familiar with this?


any pointers will be appreciated.


thanks



#pragma once
#include <iostream>
#include <string>
#include <map>
namespace pure {
class Foo { std::map<std::string *,Foo *> m; };
}
 
W

William DePalo [MVP VC++]

Ray Tayek said:
trying to put the code below into a .net class library gets unresolved
externs:


pure error LNK2020: unresolved token (0A000006) _CxxThrowException
pure error LNK2020: unresolved token (0A000015) delete
pure fatal error LNK1120: 2 unresolved externals

Often, these errors come from trying to mix native and managed code in the
same assembly and breaking the (albeit obscure) rules for doing so.

The rules are spelled out here:

http://support.microsoft.com/?id=814472
there are some clues at
http://support.microsoft.com/?kbid=814472, but it looks like a
research project.

Ah, you found the link. It's not a research project. It is pretty detailed,
no?

In a mixed-mode DLL of mine, I have this module:

#include <windows.h>
#include <_vcclrit.h>

/*********************************************************************
*********************************************************************/
void WINAPI Init(void)
{
__crt_dll_initialize();
}

/*********************************************************************
*********************************************************************/
void WINAPI Term(void)
{
__crt_dll_terminate();
}

I link the project with the /NOENTRY switch to avoid the "loader-lock"
issue, link against msvcrt.lib and ignore references from MSVCRTD or LIBCMT
(in debug and release builds respectively) and make sure that Init() is
called before the first use of any other export in the DLL and that Term()
is called before the DLL is unloaded (or the executable is terminated)
what i would like to do is to package the pure c++ code into a pure
c++ dll ideally, but *any* kind of assembly would be fine. and then
put the managed tests that nunit will find into another assembly. not
sure how to expose the public api from c to the managed code.

I think there is a disconnect here because the word DLL is in a way
overloaded. DLLs that contain assemblies are different enough from those
that export a flat API that special handling is required. (Just BTW and
IIRC, VS2005 does a better job of hiding the differences)

Regards,
Will
 
R

Ray Tayek

Often, these errors come from trying to mix native and managed code in the
same assembly and breaking the (albeit obscure) rules for doing so.

the code is all pure.
The rules are spelled out here: ...

Ah, you found the link. It's not a research project.

it is if you have been outside of the m$ world for a few years.
It is pretty detailed, no?

yes, it is detailed.
In a mixed-mode DLL of mine, I have this module:

#include <_vcclrit.h> ...
void WINAPI Init(void) ...
void WINAPI Term(void) ...
I link the project with the /NOENTRY ...

i will try this sometime.
I think there is a disconnect here because the word DLL is in a way
overloaded. DLLs that contain assemblies are different enough from those
that export a flat API that special handling is required. (Just BTW and
IIRC, VS2005 does a better job of hiding the differences)

i am sure that it does. but i am stuck with 2003 in the labs.

is your init and term stuff above for 2005 or 2003?

thanks
 
W

William DePalo [MVP VC++]

Ray Tayek said:
the code is all pure.

Hmm. Are you compiling with the /clr switch on?
it is if you have been outside of the m$ world for a few years.

Mixing and matching code from runtime environments is not often easy.
i will try this sometime.

Hmm. I thought you wanted to know how to get unmanaged C++ code and .Net
code to interoperate. My post tried to address that issue.

If don't have any experience with Nunit so I can't offer anything there.
Maybe someone else will chime in.
is your init and term stuff above for 2005 or 2003?

VS 2003. AFAIK, without resorting to tricks VS2005 only targets v2.0 of the
..Net framework. I need to support 1.1 so 2003 is it for me.

Regards,
Will
 
R

Ray Tayek

Hmm. Are you compiling with the /clr switch on?
yes


Hmm. I thought you wanted to know how to get unmanaged C++ code and .Net
code to interoperate.

they interop fine ifi build a console app (.exe).
My post tried to address that issue.

it probably does, i will try that when i get some time
If don't have any experience with Nunit so I can't offer anything there.
Maybe someone else will chime in.

the nunit works fine. my problem (right now) is linking
VS 2003. AFAIK, without resorting to tricks VS2005 only targets v2.0 of the
.Net framework. I need to support 1.1 so 2003 is it for me.

we do have 2005 in two of the labs now, so i mat try that experiment
also.

thanks
 

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