Scala c integration

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 documentation on how to do this. It would be great if it were only possible with sbt, because this is my current build system.

Currently I have heard of 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 an interface between scala and c

+7
source share
3 answers

Scala, and C - fundamentally different programming languages. You cannot expect an automatic converter from Scala to C.

If you want to call Scala code from C code or vice versa, you must use the Java Native Interface (JNI) . In this case, you will have a running JVM running your Scala code, and that the JVM will load your C code as a dynamic native library and allow it to interact with your Java / Scala code through the JNI. It is not easy though; you have to read and try a lot.

+13
source

I doubt that you will find an sbt solution for compiling C code. Even if you found an sbt plugin that compiled C code, I would be very surprised if everything was fine. Compiling native libraries is too different than compiling Scala.

To compile your C or C ++ library, I recommend CMake or Automake . None of them are perfect, but both do a good job, letting you basically declare "compile these .c or .cpp files to .so". Automake is more popular in open source projects, but CMake is much simpler. CMake also has good support for compiling on Windows if this is a requirement.

To access your native library from Scala, I recommend using JNA . It allows you to access your own code from any JVM language, including Scala. And he does it without the glue layer or code generation required by JNI .

I ported the JNA example from Java to Scala. There are a few things to note.

  • Java varargs are different from Scala varargs. If you want to call a C function variable, you will have to write an interface in Java instead of Scala. But you will use this interface just as if it were written in Scala.
    • I wanted this example to stay strictly in Scala, so I used puts instead of printf in the example
  • I made a pretty big port. You can do something for Scala -esque a bit.

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)) } } } 

On the side, it’s funny that you mention specifically, not wanting the Scala to C compiler. A recent document on compiling Scala to LLVM has been published , which would be pretty much the same as the Scala to C. compiler

+17
source

As far as I know, there is currently no way to export scala code to C. But you can write it yourself;)

+1
source

All Articles