Avoiding duplicate symbol errors during linking by removing classes from static libraries
2015-07-27 11:27
417 查看
[http://atnan.com/blog/2012/01/12/avoiding-duplicate-symbol-errors-during-linking-by-removing-classes-from-static-libraries]
Jan 12th, 2012
If you’re using a static library in your project and specifying the
are loaded correctly, you’ll likely experience “duplicate symbol” errors at link time if the static library includes the same classes used in your project or another library.
e.g. A third party has provided you with a static library (
for parsing & generating XML. You also have an internal static library (
If you use both libraries, you’ll get duplicate symbol errors for all the
A brutish yet simple workaround is to remove the offending classes from one of the libraries, so I’ll show you how it’s done.
Firstly, we need to see if the static library is a “fat file”, which means it contains code for multiple architectures. You’ll find most iOS static libraries are fat files, since they need to work both on the device (
and in the Simulator (
As you can see, we have a fat file on our hands.
The tools we’ll be using require us to split the static library into seperate files for each architecture if it’s a fat file. To do that, we’ll use
We can now use
As expected, in addition to the
we also have object files for each of the classes defined in the
object files from the archive, delete the object files we don’t want, and repack the archive.
First, the extraction:
We now have a directory containing the contents of the archive:
Simply remove the
And to confirm we’ve successfully removed the correct object files from our archive, we can list the contents again:
So far we’ve only stripped the
Now for the final step, we recombine the thin files into a fat file again:
If you’ve done everything correctly you can now replace
you won’t have any more duplicate symbol errors.
Avoiding duplicate symbol errors during linking by removing classes from static libraries
Jan 12th, 2012If you’re using a static library in your project and specifying the
-all_loadflag to ensure files containing Objective-C categories
are loaded correctly, you’ll likely experience “duplicate symbol” errors at link time if the static library includes the same classes used in your project or another library.
e.g. A third party has provided you with a static library (
libProprietary.a) which usesKissXML internally
for parsing & generating XML. You also have an internal static library (
libBitsAndBobs.a) which also uses
KissXML.
If you use both libraries, you’ll get duplicate symbol errors for all the
KissXMLclasses (
DDXMLDocument,
DDXMLElement&
DDXMLNode).
A brutish yet simple workaround is to remove the offending classes from one of the libraries, so I’ll show you how it’s done.
Firstly, we need to see if the static library is a “fat file”, which means it contains code for multiple architectures. You’ll find most iOS static libraries are fat files, since they need to work both on the device (
armv6/
armv7)
and in the Simulator (
i386).
$ lipo -info libProprietary.a Architectures in the fat file: libProprietary.a are: armv6 i386
As you can see, we have a fat file on our hands.
The tools we’ll be using require us to split the static library into seperate files for each architecture if it’s a fat file. To do that, we’ll use
lipoagain.
$ lipo -thin armv6 libProprietary.a -output libProprietary-armv6.a
We can now use
arto peek inside one of these thin archive files, listing the included object files.
$ ar -t libProprietary-armv6.a __.SYMDEF SORTED FOOBar.o DDXMLDocument.o DDXMLElement.o DDXMLElementAdditions.o DDXMLNode.o
As expected, in addition to the
FOOBar.ofile containing code specific to
libProprietary.a,
we also have object files for each of the classes defined in the
KissXMLproject. To delete those, we’ll need to unpack the
object files from the archive, delete the object files we don’t want, and repack the archive.
First, the extraction:
$ mkdir libProprietary-armv6 $ cd libProprietary-armv6 $ ar -x ../libProprietary-armv6.a
We now have a directory containing the contents of the archive:
$ ls -1 DDXMLDocument.o DDXMLElement.o DDXMLElementAdditions.o DDXMLNode.o FOOBar.o __.SYMDEF SORTED
Simply remove the
KissXMLrelated object files and repack the archive like so:
$ rm DDXML*.o $ libtool -static *.o -output ../libProprietary-armv6.a
And to confirm we’ve successfully removed the correct object files from our archive, we can list the contents again:
$ ar -t ../libProprietary-armv6.a __.SYMDEF SORTED FOOBar.o
So far we’ve only stripped the
armv6archive, so we need to repeat the process for the
i386archive:
$ lipo -thin i386 libProprietary.a -output libProprietary-i386.a $ mkdir libProprietary-i386 && cd libProprietary-i386 $ ar -x ../libProprietary-i386.a $ rm DDXML*.o $ libtool -static *.o -output ../libProprietary-i386.a
Now for the final step, we recombine the thin files into a fat file again:
lipo -create libProprietary-armv6.a libProprietary-i386.a -output libProprietary-noKissXML.a
If you’ve done everything correctly you can now replace
libProprietary.awith
libProprietary-noKissXML.aand
you won’t have any more duplicate symbol errors.
相关文章推荐
- java中类型int转化为Byte
- 【进阶android】Volley源码分析——Volley的流程
- poj 2777 Count Color(线段树、状态压缩、位运算)
- 创业公司如何公平分配股权?
- windows bat脚本编写
- (面试题)统计数组中数字的个数
- windows bat脚本编写 2015-07-27 11:27 5人阅读 评论(0) 收藏
- lucene全文检索
- hdu 5311 Hidden String dp o(n)算法 深搜
- 《实体解析与信息质量》 - 3.1.2 SERF模型
- 南阳oj 语言入门 A+B paoblem 题目477 题目844
- 高仿qq‘一键下班’—让你的view‘黏’起来
- 极客班GeekBand - 互联网思维修炼 - 1
- 配置核查模块 开发总结 (1)
- Android Dev Tool 国内网站
- 极客编程小挑战#27:生成动态大眼睛的效果
- HDOJ 题目分类
- Python开发入门与实战12-业务逻辑层
- 防止按钮在短时间被连续点击
- NYOJ 79 拦截导弹(dp)