Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way How to Compile C library into .Net dll?

Can we compile C library as .Net dll (containing and opening access to all C libs functions) by just compiling cpp project containing code like

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

with /clr:pure argument with VS? (VS10)

Or we should do something more trickey?

like image 969
Rella Avatar asked Jul 13 '10 09:07

Rella


2 Answers

This may be of interest to you: Compiling your C code to .NET

Create a C compiler occil.exe

To create a .NET dll from a c code e.g stack.c

Step1: build stack.c to IL code

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

Step2: build to generated IL code to .NET DLL

ilasm /DLL stackdll.il

Then you can reference the stack.dll in a c# program and call the C function in stack.c

like image 193
JimSEOW Avatar answered Sep 26 '22 20:09

JimSEOW


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

CLR:PURE just wont 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 the assebmly, and call Bar.Foo.foo() from .NET.

like image 27
leppie Avatar answered Sep 25 '22 20:09

leppie