Skip to content

macOS: /usr/local/lib excluded from RPath since CMake 4.0

My project links to a third-party library that I manually install under /usr/local/lib. Prior to CMake 4.0, linking to that library would cause /usr/local/lib to be added to the rpath. This stopped working with the CMAKE_OSX_SYSROOT behavior change in CMake 4.0.

It seems that the default clang 17 toolchain adds /usr/local/lib to its library search path when -isysroot is not specified:

$ /usr/bin/c++  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk  -v -Wl,-v -o foo foo.cpp
# <snip>
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 15.0.0 15.4 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -mllvm -enable-linkonceodr-outlining -o foo -v /var/folders/wt/9qgyyr3x7bn91mvwcpwt9s580000gn/T/foo-2b14f5.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.osx.a
# <snip>
Library search paths:
	/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib
	/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/swift
$ /usr/bin/c++ -v -Wl,-v -o foo foo.cpp
# <snip>
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 15.0.0 15.4 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -mllvm -enable-linkonceodr-outlining -o foo -L/usr/local/lib -v /var/folders/wt/9qgyyr3x7bn91mvwcpwt9s580000gn/T/foo-b3456a.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/lib/darwin/libclang_rt.osx.a
# <snip>
Library search paths:
	/usr/local/lib
	/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib
	/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/swift

I've observed this behavior on several machines. It looks like CMake is making a wrong assumption here that if a path is implicit for the library search path, it is also implicit for @rpath lookups, which is not the case.

Edited by Itay Perl
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information