Skip to content

[MLIR] Some tools combine dylib and static component libs (link errors on Windows, random misbehavior on Linux)Β #152371

@andrurogerz

Description

@andrurogerz

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 25d1285eecbab731eaf418c8aab44e4eb5f9e538

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions