问题描述
我想在我的swift项目中包含libgpg-error和libgcrypt并创建以下模块.modulemaps:
I want to include libgpg-error and libgcrypt in my swift-project and created the following module.modulemaps:
libgpgerror:
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 *
}
libgcrypt:
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 *
}
我还为项目和目标添加了快速编译器-搜索路径/导入路径":/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/**
.找到模块,路径正确.
I also added the "Swift Compiler - Search Path/Import Paths": /Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/**
to both project and target.The modules are found, the paths are correct.
但是,如果我要编译项目,则会出现以下错误:
However if I want to compile the project I get the following error:
ld: library not found for -l'/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/libgpgerror-1.21.dylib' for architecture x86_64
但如果我这样做
file /Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/libgpgerror-1.21.dylib
我得到了输出
/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/libgpgerror-1.21.dylib: Mach-O 64-bit dynamically linked shared library x86_64
因此,该库似乎位于正确的位置,并且具有正确的体系结构.
So it seems the library is in the correct place and also has the correct architecture.
修改
我找到了一种解决方法:我从modulemaps中删除了link-directive并手动链接了库.这似乎可行.但是为什么呢?
I found a workaround: I removed the link-directive from the modulemaps and linked the libraries manually; this seems to work. But why?
module libgpgerror {
header "/Volumes/Xcode/Programme/Swifts/KCAnon/KCAnon_Client/Libs/libgpgerror/gpg-error.h"
export *
}
推荐答案
link伪指令仅指定链接库的名称.也就是说,它应该为库指定链接器标志的后缀.看来该指令采用"-l"并将其名称连接起来以产生链接器标志.
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 *
}
这将生成链接器标志-lgcrypt
,它是正确的链接器标志.
This will generate the linker flag -lgcrypt
which is the correct linker flag.
但是,还有另一个问题是,链接器需要能够找到用于gcrypt的dylib文件,并且默认情况下,它仅在某些路径上查找.这些路径可以通过运行clang -Xlinker -v
找到.对我来说,输出看起来像这样:
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.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 ...
现在我不确定,但是我怀疑正常的搜索路径可能是
Now I'm not sure, but I suspect that the normal search paths are probably
/usr/lib
/usr/local/lib
,但我认为Xcode更改了我的搜索路径以指向MacOSX10.11.sdk/usr/lib
,顺便说一下,该文件与/usr/lib
基本上具有相同的文件集(它们没有符号链接).实际上,在El Capitan中,由于系统完整性保护,甚至sudo都不允许您编辑/usr/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
.
因此,我遇到的问题是,即使我已将库安装到/usr/local/lib
,clang也无法链接它们.为了解决这个问题,我可以明确指定搜索路径.
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/
我们要去参加比赛了.我什至可以生成一个xcodeproj,它将在Other Linker Flags
中已经设置了适当的链接器标志.
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 *
}
像这样
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!
这篇关于Xcode"ld:找不到架构x86_64的库"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!