# Cross-compilation for Windows?

**URL:** <https://discuss.tvm.apache.org/t/cross-compilation-for-windows/7698>\
**Category:** Questions\
**Created:** [August 23, 2020, 12:30pm UTC](https://discuss.tvm.apache.org/t/cross-compilation-for-windows/7698 "2020-08-23T12:30:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![myuniqueusername](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/myuniqueusername/32/1926_2.png) [@myuniqueusername](https://discuss.tvm.apache.org/u/myuniqueusername)\
**Post date:** [August 23, 2020, 12:30pm UTC](https://discuss.tvm.apache.org/t/cross-compilation-for-windows/7698/1 "2020-08-23T12:30:57Z")

</div>

Dear community people,

I have no problems with TVM running natively on Linux (and it works great!), but now I’m forced to use TVM for delpoyment on Windows. It looks like I’m missing something in my appempt to cross-compile, and therefore kindly asking for a help please.

I’ve built llvm/clang both native and cross-compilation toolchains (x86\_64-unknown-linux-gnu and x86\_64-w64-mingw32) using the following guide: [https://github.com/mstorsjo/llvm-mingw](https://github.com/mstorsjo/llvm-mingw). I defined both ‘target’ and ‘target\_host’ as ‘llvm -mtriple=x86\_64-w64-mingw32’. But when it comes to

`lib.export_library ('/tmp/deploy_lib.dll', cc.cross_compiler ("x86_64-w64-mingw32-clang++",output_format="dll"))`

the compilation command

`x86_64-w64-mingw-clang++ --shared -fPIC -o /tmp/deploy_lib.dll /tmp/tmpxxx/lib0.o`

fails because the lib0.o object file is in Linux ELF format. This lib0.o file is created by llvm\_module.cc ‘SaveToFile’ method which runs a llvm pass. What I can’t understand is how can I force it to create the object file in the PE format I need for cross-compiling for the exported dll?

I’d greatly appreciate any help.

Sincerely, Lev.

---

<div class="post-metadata">

**Author:** ![myuniqueusername](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/myuniqueusername/32/1926_2.png) [@myuniqueusername](https://discuss.tvm.apache.org/u/myuniqueusername)\
**Post date:** [August 24, 2020, 12:17pm UTC](https://discuss.tvm.apache.org/t/cross-compilation-for-windows/7698/2 "2020-08-24T12:17:53Z")

</div>

Solved: I was confused by LLVM’s triple vs target – ther’re not the same (though, sometimes they are :-)). For example:

`$ clang -target x86_64-w64-mingw32 -v 2>&1 | grep Target`

`Target: x86_64-w64-windows-gnu`

Therefore, the correct “triple” in my case should have been: x86\_64-w64-windows-gnu (and not x86\_64-w64-mingw32)

---

<div class="post-metadata">

**Author:** ![haiyang-tju](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/haiyang-tju/32/2304_2.png) [@haiyang-tju](https://discuss.tvm.apache.org/u/haiyang-tju)\
**Post date:** [June 25, 2021, 9:50am UTC](https://discuss.tvm.apache.org/t/cross-compilation-for-windows/7698/3 "2021-06-25T09:50:29Z")

</div>

Could you give the steps of cross-compilation? Thanks very much!

---

<div class="post-metadata">

**Author:** ![myuniqueusername](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/myuniqueusername/32/1926_2.png) [@myuniqueusername](https://discuss.tvm.apache.org/u/myuniqueusername)\
**Post date:** [June 27, 2021, 5:28am UTC](https://discuss.tvm.apache.org/t/cross-compilation-for-windows/7698/4 "2021-06-27T05:28:48Z")

</div>

I used an excelent LLVM MinGW recipe for building a LLVM/Clang/LLD based mingw-w64 toolchain for cross-compilation. It is available at [GitHub - mstorsjo/llvm-mingw: An LLVM/Clang/LLD based mingw-w64 toolchain](https://github.com/mstorsjo/llvm-mingw) Having built it you can easily create your Windows dll under Linux as:

```auto
graph, lib, params = relay.build (mod, target = target, target_host = target_host, params = params)
cross_compiler_path = "~/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang++"
lib.export_library (path_lib, tvm.contrib.cross_compiler (cross_compiler_path, output_format = "dll"))

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![haiyang-tju](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/haiyang-tju/32/2304_2.png) [@haiyang-tju](https://discuss.tvm.apache.org/u/haiyang-tju)\
**Post date:** [June 28, 2021, 2:03am UTC](https://discuss.tvm.apache.org/t/cross-compilation-for-windows/7698/5 "2021-06-28T02:03:13Z")

</div>

Thank you so much for your reply, this is very useful~
