Method How to compile C library in .Net dll?

Can we compile the C library as a .net dll (containing and allowing access to all C libs functions) by simply compiling a cpp project containing code like

extern "C" {
#include <library.h>
}

with an argument /clr:purewith VS? (VS10)

Or should we do something more tricks?

+5
source share
4 answers

I found it better to use the old Managed C ++ style for this.

CLR:PURE just doesn't cut it.

Example:

extern "C" int _foo(int bar)
{
  return bar;
}

namespace Bar
{
  public __gc class Foo
  {
  public:
    Foo() {}

    static int foo(int bar)
    {
      return _foo(bar);
    }
  };
};

Compile with: /clr:oldSyntax

Now you can reference assebmly and call Bar.Foo.foo()from .NET.

+3
source

This may interest you: Compiling your C code in .NET

C occil.exe

dll.NET c, , stack.c

1: stack.c IL-

occil /ostackdll.il /c /Wd /9 /NStackLib.Stack stack.c

2: IL .NET DLL

ilasm /DLL stackdll.il

stack.dll # C stack.c

+4

C.

P/Invoke , IMO . , , .

C /clr: pure :

( ) , .

+3

, , C- ++ - . C ++, ++/CLI ( clr: pure).

- , () .

++ ( ..), .

Thus, the basic information that you can compile C ++ in .NET assemblies using the / clr: xxx options is correct, but this does not mean that this is the only thing you need to do to get a useful .NET assembly.

+1
source

All Articles