我想有条件地链接一个库(我有这个库用于iOS设备,但我没有它用于模拟器).我正在使用XCode 4.6和iOS 6.1.
我读了一个问题(和几个相似的问题):iOS conditional link static library
-weak_library链接器标志
我试着使用以下标志构建项目:
-weak_library LibraryNameWithPath
但是它给了我一个错误:
ld: file not found: LibraryNameWithPath
-weak-l链接器标志
我尝试用以下标志构建它:
-weak-lShortLibraryName
得到了相同的结果:
ld: library not found for -lShortLibraryName
思考
有没有办法在构建时间内进行条件链接(与dlopen,dlclose和朋友的运行时使用相比)?
解决方法
我实际上没有尝试直接使用构建标志,但我已经使用Xcode GUI设置完成了这项工作.选择构建目标,然后选择Build Phases,然后选择将静态库添加到要链接的二进制文件列表中.
但是,从右侧的required / Optional菜单中选择Optional(不是默认值).
由于这是一个你正在谈论的静态库,我认为你需要在你的代码中加入一些预处理器保护,以禁止在模拟器中使用该库:
#if TARGET_IPHONE_SIMULATOR NSLog(@"do nothing here!"); #else HelloLibrary* hl = [[HelloLibrary alloc] init]; NSString* result = [hl helloLibraryFoo]; #endif
我没有做任何其他工作(没有修改其他构建设置).
在为模拟器构建时,我只是收到此警告:
ld: warning: ignoring file /Users/me/Desktop/code/MyAppName/libHelloLibrary.a,file was built for archive which is not the architecture being linked (i386): /Users/me/Desktop/code/MyAppName/libHelloLibrary.a