<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64150>64150</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[MLIR] Build broken on Windows with GNU tools.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
carlo-bramini
</td>
</tr>
</table>
<pre>
I tried to build mlir on Windows, but I got many errors on undefined references when linking `mlir-tblgen.exe`:
![llvm-mlir](https://github.com/llvm/llvm-project/assets/30959007/b6b25398-ff4b-41e4-8514-6f90c3365501)
If you look the attached screenshot, you can see that I have highlighted `lib/libMLIRTableGen.a`.
This is a static library and it is the cause of the problem, because it has not its dependencies satisfied.
By looking into the generated build.ninja file, I have found this line, which provides the above libraries when linking:
```
LINK_LIBRARIES = lib/libLLVMCodeGenTypes.dll.a lib/libMLIRTblgenLib.a lib/libLLVMTableGen.dll.a lib/libLLVMSupport.dll.a lib/libLLVMDemangle.dll.a lib/libMLIRTableGen.a
```
I just did a quick hack to this file, by changing the above line to:
```
LINK_LIBRARIES = lib/libMLIRTblgenLib.a lib/libMLIRTableGen.a lib/libLLVMCodeGenTypes.dll.a lib/libLLVMTableGen.dll.a lib/libLLVMSupport.dll.a lib/libLLVMDemangle.dll.a
```
After that, `mlir-tblgen.exe` has been linked successfully.
Actually, the libraries should respect some order when linking, something like:
```
static_lib1.a import_deps_for_lib1.dll.a static_lib2.a import_deps_for_lib2.dll.a ...etc
```
or all static libraries and then all import libraries if you prefer.
It is interesting to notice that `lib/libMLIRTblgenLib.a` and `lib/libMLIRTableGen.a` cannot be swapped, `lib/libMLIRTblgenLib.a` must be written before `lib/libMLIRTableGen.a`, otherwise you will got lot of undefined references again.
Evidently, one static library is also a dependency for the other one.
MSVC does not seem to suffer about this behaviour on libraries at link time.
I configured the sources according to the guidelines, by using this command line:
```
cmake ../a/llvm-project/llvm -G Ninja -DCMAKE_BUILD_TYPE=Release -Wno-dev -DBUILD_SHARED_LIBS=ON -DLLVM_ENABLE_PROJECTS="lld;clang;clang-tools-extra;bolt;cross-project-tests;polly;libclc;mlir"
```
This effect happens with GCC for CYGWIN and MinGW-w64 cross compiler.
I'm using `-DBUILD_SHARED_LIBS=ON` on the command line since this is the only way to bypass issue #60659 for me.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vk1v2zAS_TX0ZSBBlizZPvgQfyTrrZMWSdqgJ4MURxYbitSSVFz_-wUp5bOpF7vAAkEEeSjOzJs3b4ZaKw4KcUHyJcnXI9q5WptFSY3UETO0EUqMmOanxRacEcjBaWCdkBwaKQxoBQ9CcX20JF0B6xxs4aAdNFSdAI3RxvozneJYCYUcDFZoUJVo4VijAinUo1AHIEXiL4wckwdUMf5GUiQkuyDJmiTP_9MxyZdSPjWRP0vyNUlntXOt9QfTS5JeHoSrOxaXuiHppT85PKLW6F9YOpJeUmvRWZJeZsk8nyfJlKSXrGBpns1nUVVNWDQZ4ySa5eNJVFTzpMyyIs-TMUnnb4PZVnDSHUitH8HVCNQ5WtbIwZYGUdlaOw-JP1NSBRYRXE09PjV9QqjFoZbiUDvkPncpmI9UsOvd9vaeMolXqGJKiiTu3d3XwoKwQME66kQJUjBDzQmo4iCcN_koStpZBF2Fl9ZoJrEJlcHeIhzU1ILSDoSzwLFFxVGVAi1Y6oStBPLB5fIUsvPVEcrpcOUBFRrqgw4kiJVQvyhUQqL3MuRW6U5xcD5iKVSwHGtR1j6gJ8GxD5Uy_YRDGuIDHT4WvkiGv_C629582e-2y9uL2-3mDki2hhf8drsf1yvNPXz3pxZtzKWMKcB7gAPJdoLF9P2XL8h__Mob77q21cYNtnemNTZUHSR-7u21nJ9lM9AJfnXWARccKPyrE-Uj1LR8hIC7sC8QsxOUNVUHX5W3KCoEp_932P6KyvsE_iPO_wc0z4B2UTk0oa08NJ9KSKA7w4Favj27skRrq07K00D0i9J1VMqTv8Rj-spJW-t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