scala-cli icon indicating copy to clipboard operation
scala-cli copied to clipboard

`multiple definition` error when compiling native code

Open armanbilge opened this issue 2 years ago • 1 comments

Version(s) 0.1.12

Describe the bug Following the instructions for creating Scala Native application with Native code doesn't work due to a multiple definition error.

http://www.scala-native.org/en/latest/user/sbt.html#applications-with-native-code

To Reproduce resources/scala-native/bug.c

int foo(int i) {
  return i + 42;
}

bug.scala

//> using platform "native"
//> using resourceDir "./resources"

import scalanative.unsafe.*

@extern
def foo(int: CInt): CInt = extern

@main def main =
  println(foo(3))
$ scala-cli bug.scala 
Compiling project (Scala 3.1.3, Scala Native)
Compiled project (Scala 3.1.3, Scala Native)
[info] Linking (2575 ms)
[info] Discovered 665 classes and 3691 methods
[info] Optimizing (debug mode) (2722 ms)
[info] Generating intermediate code (1956 ms)
[info] Produced 16 files
[info] Compiling to native code (2162 ms)
[error] /usr/bin/ld: /workspace/sandbox/.scala-build/project_3e87ab6122_3e87ab6122-45290ff20c/native/native-code-resources-1/scala-native/bug.c.o: in function `foo':
[error] bug.c:(.text+0x0): multiple definition of `foo'; /workspace/sandbox/.scala-build/project_3e87ab6122_3e87ab6122-45290ff20c/native/native-code-main-0/scala-native/bug.c.o:bug.c:(.text+0x0): first defined here
[error] /usr/bin/ld: /workspace/sandbox/.scala-build/project_3e87ab6122_3e87ab6122-45290ff20c/native/native-code-main14358077217882765813-6/scala-native/bug.c.o: in function `foo':
[error] bug.c:(.text+0x0): multiple definition of `foo'; /workspace/sandbox/.scala-build/project_3e87ab6122_3e87ab6122-45290ff20c/native/native-code-main-0/scala-native/bug.c.o:bug.c:(.text+0x0): first defined here
[error] clang: error: linker command failed with exit code 1 (use -v to see invocation)
Exception in thread "main" scala.scalanative.build.BuildException: Failed to link /workspace/sandbox/.scala-build/project_3e87ab6122_3e87ab6122-45290ff20c/native/main
        at scala.scalanative.build.LLVM$.$anonfun$link$3(LLVM.scala:139)
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
        at scala.scalanative.build.Logger.time(Logger.scala:35)
        at scala.scalanative.build.Logger.time$(Logger.scala:32)
        at scala.scalanative.cli.utils.FilteredLogger.time(FilteredLogger.scala:5)
        at scala.scalanative.build.LLVM$.link(LLVM.scala:134)
        at scala.scalanative.build.Build$.$anonfun$build$1(Build.scala:97)
        at scala.scalanative.build.Logger.time(Logger.scala:35)
        at scala.scalanative.build.Logger.time$(Logger.scala:32)
        at scala.scalanative.cli.utils.FilteredLogger.time(FilteredLogger.scala:5)
        at scala.scalanative.build.Build$.build(Build.scala:55)
        at scala.scalanative.cli.ScalaNativeLd$.$anonfun$runLd$3(ScalaNativeLd.scala:71)
        at scala.scalanative.util.Scope$.apply(Scope.scala:32)
        at scala.scalanative.cli.ScalaNativeLd$.runLd(ScalaNativeLd.scala:70)
        at scala.scalanative.cli.ScalaNativeLd$.main(ScalaNativeLd.scala:44)
        at scala.scalanative.cli.ScalaNativeLd.main(ScalaNativeLd.scala)
Error: scala.build.errors.ScalaNativeBuildError: Error compiling with Scala Native
For more details, please see '/workspace/sandbox/.scala-build/stacktraces/1661433802-2294276439271201219.log'

Expected behaviour

It should work :)

armanbilge avatar Aug 25 '22 13:08 armanbilge

Sounds like it may be fixed by https://github.com/VirtusLab/scala-cli/pull/812? :)

armanbilge avatar Aug 25 '22 13:08 armanbilge

Sounds like it may be fixed by #812? :)

Yes, it should be fixed in Scala CLI 0.1.14

resources/scala-native/bug.c

int foo(int i) {
  return i + 42;
}

bug.scala

//> using platform "native"
//> using resourceDir "./resources"

import scalanative.unsafe.*

@extern
def foo(int: CInt): CInt = extern

@main def main =
  println(foo(3))
$ scala-cli bug.scala 
Compiling project (Scala 3.2.0, Scala Native)
Compiled project (Scala 3.2.0, Scala Native)
[info] Linking (1885 ms)
[info] Discovered 666 classes and 3692 methods
[info] Optimizing (debug mode) (1437 ms)
[info] Embedded resource: /LICENSE
[info] Embedded resource: /NOTICE
[info] Embedded resource: /library.properties
[info] Embedded resource: /rootdoc.txt
[info] Embedded resource: /META-INF/MANIFEST.MF
[info] Generating intermediate code (1091 ms)
[info] Produced 8 files
[info] Compiling to native code (3460 ms)
[info] Linking native code (immix gc, none lto) (194 ms)
[info] Total (8203 ms)
45

lwronski avatar Sep 14 '22 08:09 lwronski