V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sadhen
V2EX  ›  Scala

闲话 Scala Native: 初次见面,请多多关照

  •  
  •   sadhen · 2019-03-01 21:57:54 +08:00 · 4988 次点击
    这是一个创建于 2093 天前的主题,其中的信息可能已经有所发展或是发生改变。

    首先用 scala-native 的 g8 模板创建一个脚手架:

    $ sbt new scala-native/scala-native.g8
    ...
    A minimal project that uses Scala Native. 
    
    name [Scala Native Seed Project]: scala-native-playground
    
    Template applied in /path/to/scala-native-playground
    

    只要输入你想创建的工程的名字,整个脚手架就搭建起来了。

    我们试着编译运行一下这个脚手架:

    $ cd scala-native-playground
    $ sbt
    sbt:scala-native-playground> compile
    [info] Non-compiled module 'compiler-bridge_2.11' for Scala 2.11.12. Compiling...
    [info]   Compilation completed in 6.179s.
    [info] Done compiling.
    [success] Total time: 177 s, completed 2019-2-24 10:24:54
    sbt:scala-native-playground> run
    [error] no clang60, clang-6.0, clang50, clang-5.0, clang40, clang-4.0, clang39, clang-3.9, clang38, clang-3.8, clang37, clang-3.7, clang found in $PATH. Install clang ( http://www.scala-native.org/en/latest/user/setup.html)
    [error] no clang++60, clang++-6.0, clang++50, clang++-5.0, clang++40, clang++-4.0, clang++39, clang++-3.9, clang++38, clang++-3.8, clang++37, clang++-3.7, clang++ found in $PATH. Install clang ( http://www.scala-native.org/en/latest/user/setup.html)
    [error] (nativeClang) no clang60, clang-6.0, clang50, clang-5.0, clang40, clang-4.0, clang39, clang-3.9, clang38, clang-3.8, clang37, clang-3.7, clang found in $PATH. Install clang ( http://www.scala-native.org/en/latest/user/setup.html)
    [error] (nativeClangPP) no clang++60, clang++-6.0, clang++50, clang++-5.0, clang++40, clang++-4.0, clang++39, clang++-3.9, clang++38, clang++-3.8, clang++37, clang++-3.7, clang++ found in $PATH. Install clang ( http://www.scala-native.org/en/latest/user/setup.html)
    [error] Total time: 0 s, completed 2019-2-24 10:26:01
    

    能正常编译,但是运行的时候会报错。按照提示,我们需要安装一些依赖:

    $ sudo apt install clang libunwind-dev
    

    然后,在重新在 sbt 的 REPL 里面敲 Run 就可以了。大概是这样:

    sbt:scala-native-playground> run
    [info] Linking (534 ms)
    [info] Discovered 800 classes and 4709 methods
    [info] Optimizing (debug mode) (742 ms)
    [info] Generating intermediate code (242 ms)
    [info] Produced 39 files
    [info] Compiling to native code (332 ms)
    [info] Linking native code (immix gc) (60 ms)
    Hello, world!
    [success] Total time: 3 s, completed 2019-2-24 10:27:45
    

    整个过程还是比较流畅的,没有遇到特别浪费时间的事情,by the way,我所用的 Scala Native 版本是 0.3.8,这一点从project/plugins.sbt可以知道。

    $ cat project/plugins.sbt                                                                                                    
    addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.3.8")
    

    ====> 点我返回文章目录 <====

    1 条回复    2019-03-01 21:58:53 +08:00
    sadhen
        1
    sadhen  
    OP
       2019-03-01 21:58:53 +08:00
    主要是为了演示一下,试用 Scala Native 是多么简单。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2935 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 08:08 · PVG 16:08 · LAX 00:08 · JFK 03:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.