vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.76k stars 2.16k forks source link

Cross Compliation from Windows for Linux target fails #2382

Open UNIcodeX opened 5 years ago

UNIcodeX commented 5 years ago

V version: V 0.1.21 69b4594 OS: Windows 8

What did you do?

// fib_rec.v
pub fn fib_rec(n int) int {
  if n <= 2 {
    return 1
  } else {
    return fib_rec(n-1) + fib_rec(n-2)
  }
}

println(fib_rec(40))

What did you expect to see? successful build of fib_rec program for Linux target.

What did you see instead?

C compiler=gcc
all .v files:
["C:\V\vlib\builtin\array.v", "C:\V\vlib\builtin\builtin.v", "C:\V\vlib\builtin\float.v", "C:\V\vlib
\builtin\hashmap.v", "C:\V\vlib\builtin\int.v", "C:\V\vlib\builtin\map.v", "C:\V\vlib\builtin\option
.v", "C:\V\vlib\builtin\string.v", "C:\V\vlib\builtin\utf8.v", "C:\V\vlib\strings\builder_c.v", "C:\
V\vlib\strings\similarity.v", "C:\V\vlib\strings\strings.v", "fib_rec.v"]
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw
32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -ldl
collect2.exe: error: ld returned 1 exit status
V error: C error. This should never happen. Please create a GitHub issue: https://github.com/vlang/v
/issues/new/choose
sdwfrost commented 5 years ago

BTW, cross compilation of the same program from Linux to Windows also seems to break:

`"/home/simon/.vmodules/vlib/builtin.o"` not found

(.vmodules is present, but no compiled modules are there).

vitalyster commented 5 years ago

If you have working cross-compiler Windows->Linux in PATH, for example x86_64-linux-gnu-gcc, you can just call v --cc x86_64-linux-gnu-gcc fib_rec.v

UNIcodeX commented 5 years ago

That doesn't work for me.

> $ v --cc x86_64-w64-mingw32-gcc fibrec.v                                      
/usr/lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -ldl
collect2: error: ld returned 1 exit status
V error: C error. This should never happen. Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose

Incidentally, to test, since it's a similar project, I tried cross-compilation with Nim via nim c -d:release -d:mingw --opt:speed fib_rec.nim and the resultant executable runs properly in both wine and on a test Windows 8 box.

This at least lets me know that my machine has the proper toolchain to build cross platform executables.

vitalyster commented 5 years ago

Yes, something broken for now, but here is the trick: v -os windows -o fib_rec.c fib_rec.v (-os windows will do correct C preprocessing there) x86_64-w64-mingw32-gcc fib_rec.c -o fib_rec.exe

UNIcodeX commented 5 years ago

That does work. Thanks for the elaboration.

EDIT: The executable is created, and can be executed, but never prints the output... :thinking:

output of ldd fib_rec.exe

$ ldd ./fib_rec.exe
        ntdll.dll => /c/Windows/SYSTEM32/ntdll.dll (0x7ff827db0000)
        KERNEL32.DLL => /c/Windows/system32/KERNEL32.DLL (0x7ff827ab0000)
        KERNELBASE.dll => /c/Windows/system32/KERNELBASE.dll (0x7ff825290000)
        msvcrt.dll => /c/Windows/system32/msvcrt.dll (0x7ff826440000)
vitalyster commented 5 years ago

Known issue with msvcrt.dll, you need to configure mingw with gcc specs to link msvcr90 or later, or comment out _O_U8TEXT line in V code and rebuild (and obviously loose utf-8 support)

UNIcodeX commented 5 years ago

Alright. Thanks for the info.

lujfsd commented 4 years ago

BTW, cross compilation of the same program from Linux to Windows also seems to break:

`"/home/simon/.vmodules/vlib/builtin.o"` not found

(.vmodules is present, but no compiled modules are there).

I got the same issues, counld you share your idea?

ArtemkaKun commented 1 year ago

Actual V (0.3.2) has another problem with this code, but with Clang compiler. Logged here - #17032

felipensp commented 1 month ago

Can you try again?