0

I'm trying to compile a program written in C++ (SuitSparse, does sparse matrix calculations). I need to compile it to be used by a csharp.net application. What's the best way to do this?

1) Compile it and reference it like any other c++ dll? 2) is there any program that can convert this to clr (or something .net would understand better) 3) ??

user1167650
  • 3,177
  • 11
  • 34
  • 46
  • might be helpful: http://stackoverflow.com/questions/935664/possible-to-call-c-code-from-c – Colin D Aug 02 '12 at 19:41
  • possible duplicate of [Using C++ Class DLL in C# Application](http://stackoverflow.com/questions/569603/using-c-class-dll-in-c-sharp-application) – Kiril Aug 02 '12 at 19:46
  • .net supports c++. (at least the tooltip for the .net tag on SO mentions it). – Colin D Aug 02 '12 at 19:52
  • well, my questions is more about what's he best way to use the source code...i don't necessarily have to create a c++ dll – user1167650 Aug 02 '12 at 20:26
  • I wouldn't make a C++ DLL, I would statically link the C++ code into your C++/CLI project. – Kiril Aug 02 '12 at 22:13

1 Answers1

1

You can't use C++ DLLs in C# programs, but you can use C++/CLI DLLs in C# programs. Create a C++/CLI wrapper for the C++ project and build it into a DLL.

The alternative is to use COM interfaces, as Ed S. pointed out, but I think that doesn't provide a very C#-ish library feel. It really depends on what you feel most comfortable with, I would assume C# dlls.

Community
  • 1
  • 1
Kiril
  • 39,672
  • 31
  • 167
  • 226
  • what about P/Invoke? http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx – Colin D Aug 02 '12 at 19:49
  • Incorrect. You can reference native DLL's directly from a C# project if it exposes COM interfaces. – Ed S. Aug 02 '12 at 20:10
  • is creating the c++ dll and wrapper using the source the only way to use it from .net? (hopefully this is not too dumb of a question) – user1167650 Aug 02 '12 at 20:31
  • @EdS. COM seems like it's not the most natural way to interact with a library, I feel like a DLL would be much more natural for a C# developer. – Kiril Aug 02 '12 at 22:05
  • @ColinD yes, what about P/Invoke? – Kiril Aug 02 '12 at 22:05
  • @user1167650 you can either create a C++ DLL or statically link the C++ code into your C++/CLI wrapper. I prefer statically linking, rather than making a C++ DLL, because if you make a C++ DLL it's much harder to export classes and it's going to cause all sorts of headaches for you. – Kiril Aug 02 '12 at 22:08
  • @Lirik: I was simply responding to your assertion that "You can't use C++ DLLs in C# programs,", which is incorrect. Also, COM types can be used as if they were C# types, so it's not too bad really. – Ed S. Aug 03 '12 at 00:33