471,052 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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.


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

#pragma once

using namespace System;

namespace clrassembly {

class MathFunctions
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
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 3668
9,065 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.



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
9,065 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


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)
  3. #include "clrassembly.h"
  5. namespace clrassembly {
  7. double MathFunctions::Multiply(double a, double b){return a * b;}
  9. }
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 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.