我刚刚花了大约一周的时间来搞清楚如何从C#执行C++代码作为我日常工作的一部分.我们花了很长时间才弄明白,但最终的解决方案相当简单.
现在我很好奇......从C#调用Haskell会有多难?(注意:这是从 C#调用Haskell ,而不是相反.所以主要的可执行文件是C#.)
如果它真的很难,我不会打扰.但如果它相当容易,我可能不得不玩它...
基本上,我们编写了一些C++代码.在Windows上,它被编译成一个DLL,在Linux上它被编译成一个共享对象(*.so
).然后在C#端你做一个DllImport
并写一些手动内存管理代码,如果你试图通过任何非常重要的事情.(例如,数组,字符串等)
我知道GHC应该支持在两个平台上构建共享库,但我不确定技术细节.导出内容的语法是什么,调用者是否必须先做一些特殊的事情来初始化DLL?
具体来说:假设存在一个函数foobar :: FilePath -> IO Int32
.有人可以把一个小草图放在一起显示:
foobar
.我不太担心C#端的实际语法; 我想我或多或少地对此感到困惑.
PS我做过简要介绍hs-dotnet
,但这似乎是Windows特定的.(即,不适用于Mono,因此无法在Linux上运行.)
ham*_*mar 51
就这两种语言而言,你基本上可以假装你正试图与C代码接口.
这是一个复杂的主题,所以我不会尝试解释所有这些,而是专注于创建一个简单的示例,您可以使用下面链接的资源进行构建.
首先,您需要为Haskell函数编写包装器,这些函数使用Foreign.C.*
模块中的类型而不是通常的haskell类型.CInt
而不是Int
,CString
而不是String
等.这是最复杂的步骤,尤其是当您必须处理用户定义的类型时.
您还必须foreign export
使用ForeignFunctionInterface
扩展名为这些函数编写声明.
{-# LANGUAGE ForeignFunctionInterface #-}
module Foo where
import Foreign.C.String
import Foreign.C.Types
foreign export ccall
foo :: CString -> IO CInt
foo :: CString -> IO CInt
foo c_str = do
str <- peekCString c_str
result <- hs_foo str
return $ fromIntegral result
hs_foo :: String -> IO Int
hs_foo str = do
putStrLn $ "Hello, " ++ str
return (length str + 42)
Run Code Online (Sandbox Code Playgroud)然后,在编译时,告诉GHC创建一个共享库:
$ ghc -O2 --make -no-hs-main -optl '-shared' -o Foo.so Foo.hs
Run Code Online (Sandbox Code Playgroud)在C#方面,除了导入要调用的函数之外,还必须先导入hs_init()
并调用它来初始化运行时系统,然后才能调用任何Haskell函数.你也应该hs_exit()
在完成后打电话.
using System;
using System.Runtime.InteropServices;
namespace Foo {
class MainClass {
[DllImport("Foo.so", CallingConvention = CallingConvention.Cdecl)]
private static extern void hs_init(IntPtr argc, IntPtr argv);
[DllImport("Foo.so", CallingConvention = CallingConvention.Cdecl)]
private static extern void hs_exit();
[DllImport("Foo.so", CallingConvention = CallingConvention.Cdecl)]
private static extern int foo(string str);
public static void Main(string[] args) {
Console.WriteLine("Initializing runtime...");
hs_init(IntPtr.Zero, IntPtr.Zero);
try {
Console.WriteLine("Calling to Haskell...");
int result = foo("C#");
Console.WriteLine("Got result: {0}", result);
} finally {
Console.WriteLine("Exiting runtime...");
hs_exit();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)现在我们编译并运行:
$ mcs -unsafe Foo.cs
$ LD_LIBRARY_PATH=. mono Foo.exe
Initializing runtime...
Calling to Haskell...
Hello, C#
Got result: 44
Exiting runtime...
Run Code Online (Sandbox Code Playgroud)
有用!
资源:
Mat*_*hid 11
作为参考,我能够在Windows下使用以下程序...
{-# LANGUAGE ForeignFunctionInterface #-}
module Fibonacci () where
import Data.Word
import Foreign.C.Types
fibs :: [Word32]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
fibonacci :: Word8 -> Word32
fibonacci n =
if n > 47
then 0
else fibs !! (fromIntegral n)
c_fibonacci :: CUChar -> CUInt
c_fibonacci (CUChar n) = CUInt (fibonacci n)
foreign export ccall c_fibonacci :: CUChar -> CUInt
Run Code Online (Sandbox Code Playgroud)
用这个编译
ghc --make -shared Fibonacci.hs
Run Code Online (Sandbox Code Playgroud)
这会产生六个文件,其中一个是HSdll.dll
.然后我将其复制到Visual Studio C#项目中,并执行以下操作:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
public sealed class Fibonacci : IDisposable
{
#region DLL imports
[DllImport("HSdll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern unsafe void hs_init(IntPtr argc, IntPtr argv);
[DllImport("HSdll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe void hs_exit();
[DllImport("HSdll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern UInt32 c_fibonacci(byte i);
#endregion
#region Public interface
public Fibonacci()
{
Console.WriteLine("Initialising DLL...");
unsafe { hs_init(IntPtr.Zero, IntPtr.Zero); }
}
public void Dispose()
{
Console.WriteLine("Shutting down DLL...");
unsafe { hs_exit(); }
}
public UInt32 fibonacci(byte i)
{
Console.WriteLine(string.Format("Calling c_fibonacci({0})...", i));
var result = c_fibonacci(i);
Console.WriteLine(string.Format("Result = {0}", result));
return result;
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
这些Console.WriteLine()
电话显然是可选的.
我还没有尝试在Mono/Linux下运行它,但它可能是相似的.
总之,它与使C++ DLL工作的难度大致相同.(即,使类型签名匹配并使编组正常工作是很难的.)
我还必须编辑项目设置并选择"允许不安全的代码".