468,478 Members | 2,392 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Linker Errors Referencing a Mixed Assembly (CLR) with Native Types

Hi *,

I'm in the process of porting an ANSI C++ project from a UNIX platform to .NET. After "getting rid of" some platform-specific stuff I managed to compile the first two libraries into mixed assemlies (DLLs) using MSVC8 (VS2005) with the /clr option. However, I get hundreds of linker errors (mostly LNK2028) because assembly B uses native types defined in assembly A.

I could easily get rid of the problem by #including the original source files in addition to the header files, but then, of course, I wouldn't need DLLs in the first place. You'll find attached at the end of this post a simple snippet of code to illustrate the problem.

Can anyone please tell me the least intrusive way to "export" the definitions for native types to be accessible to other DLLs and / or applications using the CLR.

Please remember that I try to stay as close as possible to the ANSI C++ standard for now. Since I need a single source base for both the UNIX and the .NET world (using #ifdefs), all changes need to be as platform-neutral as possible. I do not want to use .lib files because there is no need to export types to Win32-specific or other external applications.

Any help is appreciated.

Dietmar

--------------------------------------------------------------------------------------------------------
// clrassembly.h (header file for assembly A)

#pragma once

using namespace System;

namespace clrassembly {

class MathFunctions
{
public:
static double Multiply(double a, double b);
};
}
-------------------------------------------------------------------------------------------------------
// clrassembly.cpp (source file for asssembly A)

#include "clrassembly.h"

double MathFunctions::Multiply(double a, double b){return a * b;}
-------------------------------------------------------------------------------------------------------
// extassembly.h (header file for assembly B)

#pragma once

using namespace System;

namespace extassembly {

class SomeMoreMathFunctions
{
public:
double percent(double a);
};
}
-----------------------------------------------------------------------------------------------------
// extassembly.cpp (source file for assembly B)

#include "extassembly.h"

#include "clrassembly.h"
//#include "clrassembly.cpp" //uncomment this to resolve linker error

//#using "clrassembly.dll" // useless if you work with native types

using namespace clrassembly;
using namespace extassembly;

double SomeMoreMathFunctions::percent(double a) {return MathFunctions::Multiply(a, 100);}
---------------------------------------------------------------------------------------------------
Jul 19 '06 #1
3 3578
Banfa
9,052 Expert Mod 8TB
And what is the linker error(s) you would get from this?
Jul 19 '06 #2
Dear Banfa,

as I mentioned in my post, the main linker errors are LNK2028 errors (followed by LNK2019 and LNK1120). I have attached the exact messages for the example I have provided (they are in German, but I'm sure you get the picture).

By the way, if I use managed classes (i.e. public ref class MathFunctions and public ref class SomeMoreMathFunctions), all linker errors disappear if I uncomment the #using directive in my example.

Thanks,

Dietmar

-------------------------------------------------------------------------------------------------------
Verknüpfen...
extassembly.obj : error LNK2028: Nicht aufgelöstes Token (0A000092) ""public: static double __cdecl clrassembly::MathFunctions::Multiply(double,double )" (?Multiply@MathFunctions@clrassembly@@$$FSANNN@Z)" , auf das in Funktion ""public: static double __cdecl extassembly::SomeMoreMathFunctions::percent(double )" (?percent@SomeMoreMathFunctions@extassembly@@$$FSA NN@Z)" verwiesen wird.

extassembly.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: static double __cdecl clrassembly::MathFunctions::Multiply(double,double )" (?Multiply@MathFunctions@clrassembly@@$$FSANNN@Z)" in Funktion ""public: static double __cdecl extassembly::SomeMoreMathFunctions::percent(double )" (?percent@SomeMoreMathFunctions@extassembly@@$$FSA NN@Z)".

D:\dev\ms.net\primer\Debug\extassembly.dll : fatal error LNK1120: 2 nicht aufgelöste externe Verweise.
Jul 19 '06 #3
Banfa
9,052 Expert Mod 8TB
as I mentioned in my post, the main linker errors are LNK2028 errors (followed by LNK2019 and LNK1120).
Remember that without knowing the actual linker you are using the numbers are completely useless without the messages themselves. Even if you did give the actual linker it still wouldn't actually help anyone without access to that linker or it's documnetation.

OK I am going to take a guess here because

a. I don't know

and

b. I don't have time to copy your files to my computer and try it out

but here goes.

I think the error is that in your header files you declare your functions as being part of a name space but in your code files you do not defined them as part of that namespace so they actually exist in the normal name space not the delared namespace.

Change you code files like so

Expand|Select|Wrap|Line Numbers
  1. // clrassembly.cpp (source file for asssembly A)
  2.  
  3. #include "clrassembly.h"
  4.  
  5. namespace clrassembly {
  6.  
  7. double MathFunctions::Multiply(double a, double b){return a * b;}
  8.  
  9. }
  10.  
I'll leave to to convert the other source file if this works.
Jul 21 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by ralphsieminsky | last post: by
8 posts views Thread by Edward Diener | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.