scala集成
我想将我的一些 scala 代码移植到 c 并从我当前的项目中调用这个移植的代码。但我没有找到任何有关如何执行此操作的文档。如果仅通过 sbt 就可以做到这一点,那就太好了,因为那是我当前的构建系统。
目前我听说过 SNA,但没有文档,
我不是在寻找自动 scala 到 c 编译器或类似的东西。我只是不知道scala和c之间的接口怎么写
I would like to port some of my scala code to c and call this ported code from my current project. But I did not find any documentation about how to do that. It would be great if this would be possible just from sbt, because thats my current build system.
Currently I have heared about SNA, but without documentation
I am not looking for an automatic scala to c compiler or anything like that. I just don't know how to write the interface between scala and c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您是否会找到一个仅使用 sbt 的解决方案来编译 C 代码。即使您找到了一个可以编译 C 代码的 sbt 插件,如果它做得很好,我也会感到非常惊讶。与编译 Scala 相比,编译本机库有很大不同。
要编译 C 或 C++ 库,我建议使用 CMake 或 Automake。两者都不是完美的,但两者都做得很好,允许您基本上声明“将这些 .c 或 .cpp 文件编译为 .so”。 Automake 在开源项目中更受欢迎,但 CMake 更简单。如果需要的话,CMake 还对 Windows 上的编译提供良好的支持。
要从 Scala 访问本机库,我建议使用 JNA。它允许您从任何 JVM 语言(包括 Scala)访问本机代码。而且它不需要 JNI 所需的粘合层或代码生成。
我已将 JNA 示例从 Java 移植到 Scala。有几点需要注意。
puts
而不是printf
和代码:
顺便说一句,有趣的是你特别提到不想要 Scala 到 C 编译器。最近发表了一篇关于将 Scala 编译为 LLVM 的论文,这将与 Scala 到 C 编译器的效果大致相同。
I doubt that you will find a sbt-only solution for compiling C code. Even if you found an sbt plugin that did compile C code, I would be really surprised if it did it well. Compiling native libraries is just too different compared to compiling Scala.
For compiling your C or C++ library, I recommend CMake or Automake. Neither is perfect, but both do a good job of allowing you to basically declare "compile these .c or .cpp files into a .so". Automake is more popular with open source projects, but CMake is quite a bit simpler. CMake also has good support for compiling on Windows, if that's a requirement.
For accessing your native library from Scala, I recommend using JNA. It allows you to access native code from any JVM language, including Scala. And it does so without the glue layer or code generation required by JNI.
I've ported the JNA example from Java to Scala. There are a couple of things to note.
puts
instead ofprintf
in the exampleAnd the code:
As an aside, it's funny that you mention specifically not wanting a Scala to C compiler. There was a recent paper published about compiling Scala to LLVM, which would have largely the same effect as a Scala to C compiler.
Scala 和 C 是根本不同的编程语言。您不能指望有一个从 Scala 到 C 的自动转换器。
如果您想从 C 代码调用 Scala 代码,反之亦然,您应该使用 Java 本机接口 (JNI)。在这种情况下,您将有一个正在运行的 JVM 执行您的 Scala 代码,并且该 JVM 会将您的 C 代码作为动态本机库加载,并允许它通过 JNI 与您的 Java/Scala 代码进行交互。但这并不容易或直截了当;你必须大量阅读和尝试。
Scala and C are fundamentally different programming languages. You cannot expect to have an automatic converter from Scala to C.
If you want to call Scala code from C code or vice-versa, you should use the Java Native Interface (JNI). In this case, you will have a running JVM executing your Scala code, and that JVM will load your C code as a dynamic native library and allow it to interact with your Java/Scala code through JNI. It's not easy or straightforward though; you have to read and try out a lot.
据我所知,目前还没有办法将 scala 代码导出到 C。但你可以自己编写;)
As far that i know there is in the moment no way to export scala code to C. But you are free to write that by yourself ;)