从 C++ 调用 Haskell代码
我目前正在用 C++ 编写一个应用程序,发现它的一些功能用 Haskell 编写会更好。我看过从 C 代码调用 Haskell 的说明,但是是否可以用C++?
编辑:澄清一下,我正在寻找一种将 Haskell 代码编译到 g++ 可以与 C++ 中的目标代码链接的外部库的方法。
更新:我在下面为其他感兴趣的人提供了一个工作示例(也是为了让我不会忘记)。
I'm currently writing an app in C++ and found that some of its functionality would be better written in Haskell. I've seen instructions on calling Haskell from C code, but is it possible to do the same with C++?
EDIT: To clarify, what I'm looking for is a way to compile Haskell code into an external library that g++ can link with the object code from C++.
UPDATE: I've put up a working example below for anyone else interested (also so I won't forget).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于任何感兴趣的人,这是我终于开始工作的测试用例:
M.hs
test.cpp
我做了编译和编译;在我的 Windows 机器上链接。要运行的命令(按此顺序)是:
最后一个 g++ 命令的长参数列表来自运行
,然后将命令复制到显示“***链接器:”的位置(需要删除一些第一个参数) 。
结果:
To anyone interested, this is the test case that I've finally got working:
M.hs
test.cpp
I did the compiles & linking on my Windows machine. The commands to run (in this order) are:
The long list of parameters for the last g++ command is from running
and then copying the command where it says "***Linker:" (some of the first parameters need to be removed).
The result:
编辑:您还应该看到下面托默的回答。我在这里的回答描述了正在发生的事情的理论,但我可能有一些执行的细节不完整,而他的回答是一个完整的工作示例。
正如sclv所示,编译应该没有问题。那里的困难可能是链接 C++ 代码,在这里,您在链接所有需要的运行时库时会遇到一些困难。问题是 Haskell 程序需要与 Haskell 运行时库链接,而 C++程序需要与C++运行时库链接。在您引用的 Wiki 页面中,当他们
编译 C 程序时,实际上执行了两个步骤:将 C 程序编译成目标文件,然后将其链接在一起。写出来的话,会是这样的(可能不太正确,因为我不会说 GHC):
在编译 C 程序时,GHC 的行为就像 GCC(IIUC,功能上是 GCC)。然而,当链接它时,它与直接调用 GCC 所发生的情况不同,因为它还神奇地包含了 Haskell 运行时库。 G++ 的工作方式与 C++ 程序相同——当它用作链接器时,它包含 C++ 运行时库。
因此,正如我提到的,您需要以与两个运行时库链接的方式进行编译。如果您在详细模式下运行 G++ 来编译和链接程序,如下所示:
它将创建一长串有关其正在执行的操作的输出;最后将是一行输出,它在其中进行链接(使用
collect2
子程序),指示它链接到哪些库。您可以将其与编译简单 C 程序的输出进行比较,看看 C++ 有何不同;在我的系统上,它添加了 -lstdc++。因此,您应该能够像这样编译和链接您的混合 Haskell/C++ 程序:
由于您已指定
-lstdc++
,它将包含 C++ 运行时库(假设-l
是正确的 GHC 语法;您需要检查),并且因为您已链接到ghc
,所以它将包含 Haskell 运行时库。这应该会产生一个工作程序。或者,您应该能够使用 GHC 执行类似于
-v
输出调查的操作,并找出它链接到的 Haskell 运行时库(或多个库)以支持 Haskell,然后在以下情况下添加该库:将您的程序与 C++ 链接,就像您对纯 C++ 程序所做的那样。 (有关详细信息,请参阅托默的回答,因为他就是这么做的。)Edit: You should also see Tomer's answer below. My answer here describes the theory of what's going on, but I may have some of the details of execution incomplete, whereas his answer is a complete working example.
As sclv indicates, compiling should be no problem. The difficulty there is likely to be linking the C++ code, and here you will have a little bit of difficulty with getting all the needed runtime libraries linked in. The problem is that Haskell programs need to be linked with the Haskell runtime libraries, and C++ programs need to be linked with the C++ runtime libraries. In the Wiki page you reference, when they do
to compile the C program, that actually does two steps: It compiles the C program into an object file, and then links it together. Written out, that would be something like (probably not quite right, as I don't speak GHC):
GHC just acts like GCC (and, IIUC, functionally is GCC) when compiling the C program. When linking it, however, it is different from what happens if you call GCC directly, because it also magically includes the Haskell runtime libraries. G++ works the same way for C++ programs -- when it's used as a linker, it includes the C++ runtime libraries.
So, as I mentioned, you need to compile in a way that links with both runtime libraries. If you run G++ in verbose mode to compile and link a program, like so:
it will create a long list of output about what it's doing; at the end will be a line of output where it does the linking (with the
collect2
subprogram) indicating what libraries it links to. You can compare that to the output for compiling a simple C program to see what's different for C++; on my system, it adds-lstdc++
.Thus, you should be able to compile and link your mixed Haskell/C++ program like so:
There, because you've specified
-lstdc++
, it will include the C++ runtime library (assuming-l
is the right GHC syntax; you'll need to check), and because you've linked withghc
, it will include the Haskell runtime library. This should result in a working program.Alternately, you should be able to do something similar to the
-v
output investigation with GHC, and figure out what Haskell runtime library (or libraries) it links to for Haskell support, and then add that library when linking your program with C++, just as you already do for pure C++ programs. (See Tomer's answer for details of that, since that's what he did.)这是关于该主题的教程:
https://github.com/jarrett/cpphs
它涵盖了调用 Haskell从 C++ 并从 Haskell 调用 C。
This is a tutorial on the topic:
https://github.com/jarrett/cpphs
It covers calling Haskell from C++ and calling C from Haskell.
cabal 2.0 添加了“foreign-library”功能,该功能似乎解决了链接器问题,并使整个构建过程总体上更加愉快。
我整理了一个简短的示例教程 https://github.com/pdlla /haskell-ffi-cabal-foreign-library-examples
cabal 2.0 added the "foreign-library" feature which seems to solve the linker issues as well as making the whole build process much more pleasant in general.
I put together a short example tutorial https://github.com/pdlla/haskell-ffi-cabal-foreign-library-examples
由于您可以从 C 调用 Haskell,因此没有理由不能从 C++ 调用它。另一方面,从 Haskell 调用 C++ 则困难得多,并且通常需要 C 包装器。
编辑以展开。这些说明是错误的或不完整的。它们是一个维基页面。直接看GHC手册:http:// www.haskell.org/ghc/docs/6.12.2/html/users_guide/ffi-ghc.html
这描述了如何导出函数,以及如何使用自己的 main.c 文件。请注意其中写着“其他语言,比如 C”。它这样说是因为您可以从任何可以调用您正在导出的普通 C 函数以及 HsFFI.h 提供的语言(和编译器)来执行此操作。这与语言和编译器无关。它所需要的只是能够使用系统上的标准调用约定来调用 C 函数,C++ 编译器(例如 g++)当然可以提供这一功能。
Since you can call Haskell from C, there's no reason you can't call it from C++. Calling C++ from Haskell, on the other hand, is much harder and usually requires a C wrapper.
Edit to expand. The instructions are wrong to incomplete. They're a wiki page. Look directly at the GHC manual: http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/ffi-ghc.html
This describes how to export functions, and how to use your own main. Note where it says "some other language, say C." It says this because you can do this from any language (and compiler) that can invoke the vanilla C functions that you're exporting, and that HsFFI.h provides. This is language agnostic, and compiler agnostic. All it requires is the ability to invoke C functions using the standard calling conventions on your system, which a C++ compiler (such as g++) certainly provides.