'How do I fix "ld: library not found for -lubsan" while compiling with g++?

When I try to compile my eek.cpp file with g++ eek.cpp -o eek -fsanitize=undefined flag, it shows:

ld: library not found for -lubsan
collect2: error: ld returned 1 exit status

but g++ eek.cpp -o eek is working fine. My terminal screenshot here.

I'm using zsh and gcc 10.2.0 on macOS Big Sur. fsanitize=undefined was still working yesterday before I updated my macOS from Catalina.

The ubsan.h file does exist in /usr/local/Cellar/gcc/10.2.0_2/lib/gcc/10/gcc/x86_64-apple-darwin20/10.2.0/plugin/include, but it isn't working. I've also read this post but it didn't solve my problem. How should I fix it?



Solution 1:[1]

I've had the same problem since I updated my macOS, but have just now fixed with brew upgrade. I'm sorry that I can't say what formula was involved in this problem, but updating formulae would be worth trying.

Solution 2:[2]

I had similar problem when trying to compile a fortran program and getting the error ld: librarynot found for -lSystem. The answer of @kaage helped me but I don't have enough reputation to comment it.

I just wanted to add that when running brew upgrade it complained that my command line tools were too outdated. Running (as suggested by brew itself)

 sudo rm -rf /Library/Developer/CommandLineTools
 sudo xcode-select --intsall

fixed it.

Not sure this perfectly targets the question asked though so feel free to correct me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kaage
Solution 2 Tafuzzo