有条件地链接iOS上的库

Vic*_*nin 5 xcode ios

我想有条件地链接一个库(我有用于iOS设备的该库,但没有用于Simulator的库)。我正在使用Xcode 4.6和iOS 6.1。

我读了一个问题(和几个类似的问题):iOS条件链接静态库

-weak_library链接器标志

我尝试使用以下标志来构建ry项目:

-weak_library LibraryNameWithPath
Run Code Online (Sandbox Code Playgroud)

但是它给我一个错误:

ld: file not found: LibraryNameWithPath
Run Code Online (Sandbox Code Playgroud)

-weak -l链接器标志

我尝试使用以下标志构建它:

-weak-lShortLibraryName
Run Code Online (Sandbox Code Playgroud)

并得到相同的结果:

ld: library not found for -lShortLibraryName
Run Code Online (Sandbox Code Playgroud)

思想

如果它被显式标记为弱链接,为什么还要检查库是否存在?

有没有一种方法可以在构建时进行条件链接(与dlopen,dlclose和friends的运行时使用情况相比)?

Nat*_*ate 5

我实际上并没有尝试直接使用构建标志来做到这一点,但我已经使用 Xcode GUI 设置完成了它。选择您的 build Target,然后Build Phases,然后选择将您的静态库添加到要链接的二进制文件列表中。

但是,从右侧的“必需/可选”菜单中选择“可选”(这不是默认设置)。

在此处输入图片说明

由于这是您正在谈论的静态库,因此我认为您需要在代码中放置一些预处理器防护,以禁用在模拟器中使用该库:

#if TARGET_IPHONE_SIMULATOR
   NSLog(@"do nothing here!");
#else
   HelloLibrary* hl = [[HelloLibrary alloc] init];
   NSString* result = [hl helloLibraryFoo];
#endif
Run Code Online (Sandbox Code Playgroud)

我没有做任何其他事情来完成这项工作(没有修改其他构建设置)。

在为模拟器构建时,我只是收到这个警告:

ld:警告:忽略文件 /Users/me/Desktop/code/MyAppName/libHelloLibrary.a,文件是为归档而构建的,这不是正在链接的架构 (i386):/Users/me/Desktop/code/MyAppName/libHelloLibrary。一种