Xcode "ld: library not found [...] for architecture x86_64" Xcode "ld: library not found [...] for architecture x86_64" xcode xcode

Xcode "ld: library not found [...] for architecture x86_64"


The link directive specifies only the name of the linked library. That is it should specify the suffix of the linker flag for the library. It appears that the directive take "-l" and concatenates the name to produce the linker flag.

This means that the correct way to specify your module map is as follows.

module CGcrypt {    header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgcrypt/gcrypt.h"    link "gcrypt"    export *}

This will generate the linker flag -lgcrypt which is the correct linker flag.

However, there is another problem which is that the linker needs to be able to find the dylib file for gcrypt and by default it only looks on certain paths. Those paths can be found by running clang -Xlinker -v. The output for me looks like this:

tylercloutier$ clang -Xlinker -v@(#)PROGRAM:ld  PROJECT:ld64-264.3.101configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)Library search paths:    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/lib... more stuff ...

Now I'm not sure, but I suspect that the normal search paths are probably

/usr/lib/usr/local/lib

but I think that Xcode has altered my search paths to point the the MacOSX10.11.sdk/usr/lib, which, incidentally, has basically the same set of files as /usr/lib (they are not symlinked). Indeed, in El Capitan, because of System Integrity Protection even sudo will not allow you to edit /usr/lib.

Thus the problem that I am having is that even though I've installed my libraries to /usr/local/lib, clang is not able to link them. In order to fix that I can specify the search path explicitly.

swift build -Xlinker -L/usr/local/lib/

And we're off to the races. I can even generate an xcodeproj which will have the appropriate linker flag already set in Other Linker Flags.

swift build -Xlinker -L/usr/local/lib/ --generate-xcodeproj

If you leave out the link directive in the module map file, you can specify it as a flag:

module CGcrypt {    header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgcrypt/gcrypt.h"    export *}

Like so

swift build -Xlinker -L/usr/local/lib/ -lgcrypt

How to change the default library search paths, I don't know. But it would be great if someone else could shed light on this matter!