scala集成

发布于 2024-11-14 10:38:35 字数 192 浏览 2 评论 0原文

我想将我的一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦上情书 2024-11-21 10:38:35

我怀疑您是否会找到一个仅使用 sbt 的解决方案来编译 C 代码。即使您找到了一个可以编译 C 代码的 sbt 插件,如果它做得很好,我也会感到非常惊讶。与编译 Scala 相比,编译本机库有很大不同。

要编译 C 或 C++ 库,我建议使用 CMakeAutomake。两者都不是完美的,但两者都做得很好,允许您基本上声明“将这些 .c 或 .cpp 文件编译为 .so”。 Automake 在开源项目中更受欢迎,但 CMake 更简单。如果需要的话,CMake 还对 Windows 上的编译提供良好的支持。

要从 Scala 访问本机库,我建议使用 JNA。它允许您从任何 JVM 语言(包括 Scala)访问本机代码。而且它不需要 JNI 所需的粘合层或代码生成。

我已将 JNA 示例从 Java 移植到 Scala。有几点需要注意。

  1. Java 可变参数与 Scala 可变参数不同。如果您想调用可变参数 C 函数,则必须使用 Java 而不是 Scala 编写接口。但您将使用该接口,就像使用 Scala 编写的接口一样。
    • 我希望示例严格保留在 Scala 中,因此我在示例中使用了 puts 而不是 printf
  2. 我几乎直接进行了移植。您可能想让事情变得有点像 Scala 风格。

和代码:

import com.sun.jna.{Library, Native, Platform}

trait CLibrary extends Library {
  def puts(s: String)
}

object CLibrary {
  def Instance = Native.loadLibrary(
    if (Platform.isWindows) "msvcrt" else "c",
    classOf[CLibrary]).asInstanceOf[CLibrary]
}

object HelloWorld {
  def main(args: Array[String]) {
    CLibrary.Instance.puts("Hello, World");
    for ((arg, i) <- args.zipWithIndex) {
      CLibrary.Instance.puts(
        "Argument %d: %s".format(i.asInstanceOf[AnyRef], arg))
    }
  }
}

顺便说一句,有趣的是你特别提到不想要 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.

  1. Java varargs are different from Scala varargs. If you want to call a variadic C function, you'll have to write the interface in Java instead of Scala. But you will use that interface exactly like you would if it were written in Scala.
    • I wanted the example to remain strictly in Scala, so I used puts instead of printf in the example
  2. I did pretty much a straight port. You may want to make things a bit for Scala-esque.

And the code:

import com.sun.jna.{Library, Native, Platform}

trait CLibrary extends Library {
  def puts(s: String)
}

object CLibrary {
  def Instance = Native.loadLibrary(
    if (Platform.isWindows) "msvcrt" else "c",
    classOf[CLibrary]).asInstanceOf[CLibrary]
}

object HelloWorld {
  def main(args: Array[String]) {
    CLibrary.Instance.puts("Hello, World");
    for ((arg, i) <- args.zipWithIndex) {
      CLibrary.Instance.puts(
        "Argument %d: %s".format(i.asInstanceOf[AnyRef], arg))
    }
  }
}

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.

美人如玉 2024-11-21 10:38:35

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.

メ斷腸人バ 2024-11-21 10:38:35

据我所知,目前还没有办法将 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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文