I had this exact same error myself just now, here's how I solved it.
TLDR:
- update homebrew by running
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
and then brew update
- then search for any out-of-date packages using
brew outdated
- then reinstall any/all outdated packages if they are causing you any issues
- 2023 UPDATE: Homebrew appears now to perform all of these steps automatically by default, so if you simply run
brew upgrade <package>
brew will update itself, and update the requested package, and then any other outdated packages it finds
Background: So, I updated my Mac OS (Big Sur) last night. I flipped it open this morning, very unassuming, and tried to open a text file in vim (Macvim). I received an error message identical to the one above. Here is my error message in full.
dyld: Library not loaded: /System/Library/Perl/5.28/darwin-thread-multi-2level/CORE/libperl.dylib
Referenced from: /usr/local/Cellar/macvim/8.2-171/MacVim.app/Contents/MacOS/Vim
Reason: image not found
Abort trap: 6
This stack post (the one you're reading) was the first result when I searched for the error. I noted the OP found the problem in Xcode, so I knew this problem was more general than my specific case, and (obviously) had something to do with the macOS update.
The first thing I did was update my Homebrew, as per the recommendation from this webpage.
NOTE: When updating Homebrew, brew first warned me that from now on I needed to first run this command:
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
Then you simply run:
brew update
If you don't have Homebrew installed then definitely do that here
Brew (in its usual style), after the update, helpfully informed me that several of my 'formulae' were out of date. So I ran:
brew outdated
And it returned a list of outdated packages, one of which was macvim.
So, after updating brew, I ran
brew upgrade macvim
And this solved the problem for me.
I'm posting this answer to help anyone with the same problem, since you might get the error somewhere besides Xcode, and it's clear the problem is most likely after an update of the OS, and apple decides to mess around with lib
locations.
After an update, before you go chasing your tail, check that all your software is up to date.
Thanks to appuals for providing the most sensible solutions to this problem. I take no credit.
NOTE: If your problem is coming from Xcode, then you may need to update Xcode, which you would be better off doing via the App Store.