<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/152371>152371</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
link errors when building LLVM as a Windows DLL with mlir project included
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
andrurogerz
</td>
</tr>
</table>
<pre>
## Overview
When configured to build as a Windows DLL, LLVM does not build successfully when the `mlir` project is included.
```
LLVM-22git.lib(LLVM-22git.dll) : error LNK2005: "public: static class llvm::ErrorOr<class std::unique_ptr<class llvm::MemoryBuffer,struct std::default_delete<class llvm::MemoryBuffer> > > __cdecl llvm::MemoryBuffer::getFileOrSTDIN(class llvm::Twine const &,bool,bool,class std::optional<struct llvm::Align>)" (?getFileOrSTDIN@MemoryBuffer@llvm@@SA?AV?$ErrorOr@V?$unique_ptr@VMemoryBuffer@llvm@@U?$default_delete@VMemoryBuffer@llvm@@@std@@@std@@@2@AEBVTwine@2@_N1V?$optional@UAlign@llvm@@@std@@@Z) already defined in LLVMSupport.lib(MemoryBuffer.cpp.obj)
LLVM-22git.lib(LLVM-22git.dll) : error LNK2005: "public: __cdecl llvm::CleanupInstaller::~CleanupInstaller(void)" (??1CleanupInstaller@llvm@@QEAA@XZ) already defined in LLVMSupport.lib(ToolOutputFile.cpp.obj)
LLVM-22git.lib(LLVM-22git.dll) : error LNK2005: "public: __cdecl llvm::ToolOutputFile::ToolOutputFile(class llvm::StringRef,class std::error_code &,enum llvm::sys::fs::OpenFlags)" (??0ToolOutputFile@llvm@@QEAA@VStringRef@1@AEAVerror_code@std@@W4OpenFlags@fs@sys@1@@Z) already defined in LLVMSupport.lib(ToolOutputFile.cpp.obj)
```
The duplicate symbols are exported by the LLVM DLL and by a static library linked as a transitive dependency.
## Repro
On Windows 11:
```
git checkout 66d1c37eb69c8ab38af6e61a38b7605f5f05d75b
cmake -B build -S llvm -G Ninja -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;llvm;mlir" -DLLVM_OPTIMIZED_TABLEGEN=ON -DLLVM_BUILD_TESTS=ON -DLLVM_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DCLANG_LINK_CLANG_DYLIB=OFF -DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_BUILD_LLVM_DYLIB_VIS=ON -DLLVM_LINK_LLVM_DYLIB=ON
ninja -C build
```
Observe build errors.
```
C:\WINDOWS\system32\cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=tools\mlir\tools\mlir-src-sharder\CMakeFiles\mlir-src-sharder.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\mt.exe --manifests -- C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1439~1.335\bin\HostX64\x64\link.exe /nologo tools\mlir\tools\mlir-src-sharder\CMakeFiles\mlir-src-sharder.dir\mlir-src-sharder.cpp.obj tools\mlir\tools\mlir-src-sharder\CMakeFiles\mlir-src-sharder.dir\S_\llvm\llvm-project\llvm\resources\windows_version_resource.rc.res /out:bin\mlir-src-sharder.exe /implib:lib\mlir-src-sharder.lib /pdb:bin\mlir-src-sharder.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console lib\LLVMSupport.lib lib\LLVMTableGen.lib lib\MLIRSupport.lib psapi.lib shell32.lib ole32.lib uuid.lib advapi32.lib ws2_32.lib ntdll.lib delayimp.lib -delayload:shell32.dll -delayload:ole32.dll lib\LLVMDemangle.lib lib\LLVM-22git.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1439~1.335\bin\HostX64\x64\link.exe /nologo tools\mlir\tools\mlir-src-sharder\CMakeFiles\mlir-src-sharder.dir\mlir-src-sharder.cpp.obj tools\mlir\tools\mlir-src-sharder\CMakeFiles\mlir-src-sharder.dir\S_\llvm\llvm-project\llvm\resources\windows_version_resource.rc.res /out:bin\mlir-src-sharder.exe /implib:lib\mlir-src-sharder.lib /pdb:bin\mlir-src-sharder.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console lib\LLVMSupport.lib lib\LLVMTableGen.lib lib\MLIRSupport.lib psapi.lib shell32.lib ole32.lib uuid.lib advapi32.lib ws2_32.lib ntdll.lib delayimp.lib -delayload:shell32.dll -delayload:ole32.dll lib\LLVMDemangle.lib lib\LLVM-22git.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST:EMBED,ID=1" failed (exit code 1169) with the following output:
LLVM-22git.lib(LLVM-22git.dll) : error LNK2005: "public: static class llvm::ErrorOr<class std::unique_ptr<class llvm::MemoryBuffer,struct std::default_delete<class llvm::MemoryBuffer> > > __cdecl llvm::MemoryBuffer::getFileOrSTDIN(class llvm::Twine const &,bool,bool,class std::optional<struct llvm::Align>)" (?getFileOrSTDIN@MemoryBuffer@llvm@@SA?AV?$ErrorOr@V?$unique_ptr@VMemoryBuffer@llvm@@U?$default_delete@VMemoryBuffer@llvm@@@std@@@std@@@2@AEBVTwine@2@_N1V?$optional@UAlign@llvm@@@std@@@Z) already defined in LLVMSupport.lib(MemoryBuffer.cpp.obj)
LLVM-22git.lib(LLVM-22git.dll) : error LNK2005: "public: __cdecl llvm::CleanupInstaller::~CleanupInstaller(void)" (??1CleanupInstaller@llvm@@QEAA@XZ) already defined in LLVMSupport.lib(ToolOutputFile.cpp.obj)
LLVM-22git.lib(LLVM-22git.dll) : error LNK2005: "public: __cdecl llvm::ToolOutputFile::ToolOutputFile(class llvm::StringRef,class std::error_code &,enum llvm::sys::fs::OpenFlags)" (??0ToolOutputFile@llvm@@QEAA@VStringRef@1@AEAVerror_code@std@@W4OpenFlags@fs@sys@1@@Z) already defined in LLVMSupport.lib(ToolOutputFile.cpp.obj)
Creating library lib\mlir-src-sharder.lib and object lib\mlir-src-sharder.exp
bin\mlir-src-sharder.exe : fatal error LNK1169: one or more multiply defined symbols found
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsGU1z2zru1zAXjj0yKTnOwQdZlrve-qMbu-l77-KhRNhmQ4lakkriPeS371Af8UeS7uu8zr5LOqlFAgQIgAABQcwYscsBhigYoWB8xUq7V3rIcq5LrXag_3OVKH4YIkIRoXj5APpBwCPywm97yHGq8q3YlRo4tgonpZAcM4MZ_iZyrh4NHs9miER4NrubY67A4FzZZp0p0xSM2ZZSHvCj42b3gFHfy6TQqO_hQqvvkFosDBZ5KksOvIu80P31vebPCx3rDiE7YbtSJIgMTgBcSkRuMKIhBq2VxrPFZ-J5gQMgQooykSJ1E2OZFSlOJTMGS_mQIRoiGsaOaKkRjWqMsbxGlLn4dwmbwh5xR6o5ZEofRuV2CxqRyFhdpvZIy2HLSmk3HCRY-F_0NMbt_80m5ZDKd1c6yA7sREhY6tV6PF0gMrhkvn4UObhzMxYj0kckSpSSx8eFoqqwQuVMItrqcWQVSrHLEY0RuUGEYEQGiE4uBPC9Mxl9ryL3PeR7qxDRSXiH6AQRv7W07zWAEwv73t27TL7Wqy9s-iMK5HtOu7fGBPleGI_uKhs1882i10j0Ygrf-1qr_gO2fzi_Y1ID4wfMYSty4FjkVSCsyqJQunXXU0G7aVF0VfLdWfQXefZrp4kksLwsprmxTMrWcZ5fgcngQQl-driITnqv1p1a4V9xGCLf--3Pq79WSi5LW5SV2_xfDHC-5Zuw13Gzslrku1vYvo6Rav9Nqjg0EQV5mZ2QmoOpB9vmuSwgn0i2M5fG9S7EeMO0d0dBfK9XOWx4d5Tg1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