Xcode"ld:找不到用于架构x86_64的库"

K. *_*ann 6 xcode build shared-libraries ld

我想在我的swift项目中包含libgpg-error和libgcrypt,并创建了以下module.modulemaps:

libgpgerror:

module libgpgerror {
    header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/gpg-error.h"
    link "'/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/libgpgerror-1.21.dylib'"
    export *
}
Run Code Online (Sandbox Code Playgroud)

libgcrypt:

module libgcrypt {
    header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgcrypt/gcrypt.h"
    link "'/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgcrypt/libgcrypt-1.6.5.dylib'"
    export *
}
Run Code Online (Sandbox Code Playgroud)

我还/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/**为项目和目标添加了"Swift编译器 - 搜索路径/导入路径":找到模块,路径是正确的.

但是,如果我想编译项目,我会收到以下错误:

ld: library not found for -l'/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/libgpgerror-1.21.dylib' for architecture x86_64
Run Code Online (Sandbox Code Playgroud)

但如果我这样做

file /Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/libgpgerror-1.21.dylib
Run Code Online (Sandbox Code Playgroud)

我得到了输出

/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/libgpgerror-1.21.dylib: Mach-O 64-bit dynamically linked shared library x86_64
Run Code Online (Sandbox Code Playgroud)

因此,图书馆似乎位于正确的位置,并且具有正确的架构.


编辑

我找到了一个解决方法:我从模块映射中删除了link-directive并手动链接了这些库; 这似乎有效.但为什么?

module libgpgerror {
    header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/gpg-error.h"
    export *
}
Run Code Online (Sandbox Code Playgroud)

Tyl*_*ier 5

link指令仅指定链接库的名称.也就是说,它应该指定库的链接器标志的后缀.看起来该指令采用"-l"并连接名称以生成链接器标志.

这意味着指定模块映射的正确方法如下所示.

module CGcrypt {
    header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgcrypt/gcrypt.h"
    link "gcrypt"
    export *
}
Run Code Online (Sandbox Code Playgroud)

这将生成链接器标志-lgcrypt,这是正确的链接器标志.

但是,还有一个问题是链接器需要能够为gcrypt找到dylib文件,默认情况下它只能查找某些路径.可以通过运行找到这些路径clang -Xlinker -v.我的输出看起来像这样:

tylercloutier$ clang -Xlinker -v
@(#)PROGRAM:ld  PROJECT:ld64-264.3.101
configured 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 ...
Run Code Online (Sandbox Code Playgroud)

现在我不确定,但我怀疑正常的搜索路径可能是

/usr/lib
/usr/local/lib
Run Code Online (Sandbox Code Playgroud)

但是我认为Xcode已经改变了我的搜索路径以指向MacOSX10.11.sdk/usr/lib它,顺便提一下,它与基本相同的文件集/usr/lib(它们没有符号链接).实际上,在El Capitan中,由于系统完整性保护甚至sudo都不允许您编辑/usr/lib.

因此,我遇到的问题是即使我已经安装了我的库/usr/local/lib,clang也无法链接它们.为了解决这个问题,我可以明确指定搜索路径.

swift build -Xlinker -L/usr/local/lib/
Run Code Online (Sandbox Code Playgroud)

而我们正在参加比赛.我甚至可以生成一个xcodeproj,它已经设置了相应的链接器标志Other Linker Flags.

swift build -Xlinker -L/usr/local/lib/ --generate-xcodeproj
Run Code Online (Sandbox Code Playgroud)

如果省略模块映射文件中的link指令,则可以将其指定为标志:

module CGcrypt {
    header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgcrypt/gcrypt.h"
    export *
}
Run Code Online (Sandbox Code Playgroud)

像这样

swift build -Xlinker -L/usr/local/lib/ -lgcrypt
Run Code Online (Sandbox Code Playgroud)

如何更改默认的库搜索路径,我不知道.但如果其他人能够阐明这件事情,那就太棒了!