Ubuntu 20.04下多版本Clang/LLVM冲突致命令执行失败求助
Hey there, I get it—switching from GCC to Clang can be a hassle, especially when you run into weird library conflicts like this. Let's break down how to fix this system-wide so you can use Clang directly without build tool workarounds.
Root Cause
The error happens because your system has multiple conflicting versions of LLVM/Clang libraries installed: you've got clang-10 (the main version you want) alongside libc++-7 and its associated libraries. These old libraries are clashing with the newer LLVM 10 components, causing duplicate command-line option registrations.
System-Wide Fix Solutions
Solution 1: Clean Up Old Conflicting Libraries (Recommended)
This is the most straightforward fix—remove the outdated libc++ 7 packages that are causing the conflict, then ensure Clang 10 is set as your default:
Uninstall the old libc++ 7 packages completely:
sudo apt remove --purge libc++-7-dev libc++1-7 libc++abi-7-dev libc++abi1-7 sudo apt autoremove --purgeThe
autoremovecommand will clean up any leftover dependencies tied to these packages.Set Clang 10 as the system's default clang/clang++:
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-10 100 sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-10 100The
100here gives it priority over any other versions that might be installed.Verify the fix:
Runclang --version—you should see output for Clang 10.0.0, and the error should be gone. Test a simple compile withclang -c test.cto make sure everything works.
Solution 2: Isolate Libraries with Environment Variables (If You Need Old Versions)
If you absolutely have to keep the libc++ 7 libraries for another project, you can force Clang to only use the LLVM 10 libraries by setting the LD_LIBRARY_PATH:
Temporarily set the path for your current terminal session:
export LD_LIBRARY_PATH=/usr/lib/llvm-10/lib:$LD_LIBRARY_PATHMake it permanent by adding it to your shell config:
For Bash:echo 'export LD_LIBRARY_PATH=/usr/lib/llvm-10/lib:$LD_LIBRARY_PATH' >> ~/.bashrc source ~/.bashrcFor Zsh:
echo 'export LD_LIBRARY_PATH=/usr/lib/llvm-10/lib:$LD_LIBRARY_PATH' >> ~/.zshrc source ~/.zshrcVerify:
Runclang --versionand a test compile to confirm no more errors.
Solution 3: Full Reinstall of Clang (For Stubborn Cases)
If the above methods don't work, a full clean reinstall will wipe out all conflicting components:
Purge all Clang/LLVM related packages:
sudo apt remove --purge clang clang-10 llvm-10* libclang-10* libc++* sudo apt autoremove --purge sudo apt cleanReinstall Clang 10:
sudo apt update sudo apt install clang-10Set it as the default again:
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-10 100 sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-10 100
Final Notes
Sticking with Solution 1 is best long-term—having mismatched LLVM/libc++ versions is a recipe for future issues. Once you've fixed this, you should be able to use Clang directly from the terminal without any build tool hacks.
内容的提问来源于stack exchange,提问作者Lahiru Udayanga

