Linking not working in homebrew's cmake since

2019-06-18 03:41发布

I've reproduced this symptom on two computers now, cmake seems to no longer look in /usr/local/lib (or more properly, $(brew --prefix)/lib) for Homebrew-provided libraries since upgrading my machine to macOS Mojave.

Although there are ways to circumvent this (e.g. search for homebrew prefix using EXECUTE_PROCESS; add the result to LINK_LIBRARIES(...) command) none are ideal. What changed in Mojave to break this behavior?

The temporary workaround is to add the following to CMakeLists.txt:

# WARNING: Don't hard-code this path
LINK_DIRECTORIES(/usr/local/lib)

I've already tried brew doctor and updated all homebrew packages to no avail.

The specific error that cmake (make) shows is:

ld: library not found for -l<somelib>

I've asked the question on the Homebrew forums and the Apple developer forums.

1条回答
放荡不羁爱自由
2楼-- · 2019-06-18 04:34

I've isolated this to the following change in the VERBOSE=1 make logs...

  • High Sierra (<=10.13) and below did NOT use the -isysroot command.
  • Mojave (>=10.14) DOES use the -isysroot command.

From gnu.org:

-isysroot <dir> This option is like the --sysroot option, but applies only to header files (except for Darwin targets, where it applies to both header files and libraries). See the --sysroot option for more information.

So this flag specifically clobbers the lib search path only on Apple. This results in the compilation never looking in the standard ld locations, which can be seen by typing ld -v dummy.

Library search paths:
    /usr/lib
    /usr/local/lib

Why does cmake do this? My thought is it was to fix the /usr/local/include issues introduced with the new Mojave SDK behavior.

Unfortunately, I can't find a cmake compile flag to add the default library search paths back in. For now the only solution I've found is to add the following to my project:

IF(APPLE)
    # Fix linking on 10.14+. See https://stackoverflow.com/questions/54068035
    LINK_DIRECTORIES(/usr/local/lib)
ENDIF()

I'm not sure if this is a behavior that warrants an upstream cmake patch. If there is a better solution, please provide it.

查看更多
登录 后发表回答