如何减小 g++ 的文件大小编译共享对象?
我有一个用 g++ 编译的共享对象(.so)文件,在 Windows 中,其大小约为 2MB(.DLL,用 Visual Studio 2008,/O2 编译),但在 Linux 中,如果使用 g++ -O2 标志编译,其大小为 10MB 。
即使我使用 -Os 标志编译它,最终的 .so 文件大小仍然有 5MB。
我知道可执行文件可以通过 strip 命令减少,但它似乎不适用于 .so 文件(它可以被删除,但无法加载)。
如何减少该共享对象的文件大小?是否有共享对象的剥离命令?
编辑1:
我的g++版本是4.1.2。我在代码中使用 Boost 1.43。
我的 makefile 中的编译标志:
g++ -DNDEBUG -D_PYTHON -DBOOST_PYTHON_STATIC_LIB -I"boost_1_43_0" -I"/usr/local/include/python2.6" -fno-tree-vrp -Os -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
链接标志:
LIBS := -lm -lz -ltidy -lpng14 -lxml2 -liconv -lboost_regex-gcc41-mt-s -lboost_serialization-gcc41-mt-s -lboost_python-gcc41-mt-s -lpython2.6
这是我的共享对象的 ldd:
linux-gate.so.1 => (0x00327000)
libz.so.1 => /lib/libz.so.1 (0x004f4000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00110000)
libm.so.6 => /lib/libm.so.6 (0x00f31000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x0053b000)
libc.so.6 => /lib/libc.so.6 (0x00328000)
/lib/ld-linux.so.2 (0x0077d000)
我将尝试使用 -fno-inline 标志,但我想知道这会影响编译代码的性能。
我怀疑这是因为我静态链接到 python 2.6?我的ldd中应该有libpython2.6.so,但我没有看到它。
我的链接或编译标志有问题吗?
I have a shared object (.so) file compiled with g++, in Windows, its size about 2MB (.DLL, compiled with Visual Studio 2008, /O2), but in Linux, its size is 10MB if compiled with g++ -O2 flag.
Even I compile it with -Os flag, the final .so file size still have 5MB.
I know the executable can be reduced by strip command, but it seems not working with .so file (it can be stripped, but unable to load).
How can I reduce file size of this shared object? Are there any strip command for shared object?
Edit1:
My g++ version is 4.1.2. I use Boost 1.43 in my code.
The compile flags in my makefile:
g++ -DNDEBUG -D_PYTHON -DBOOST_PYTHON_STATIC_LIB -I"boost_1_43_0" -I"/usr/local/include/python2.6" -fno-tree-vrp -Os -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "lt;"
The link flags:
LIBS := -lm -lz -ltidy -lpng14 -lxml2 -liconv -lboost_regex-gcc41-mt-s -lboost_serialization-gcc41-mt-s -lboost_python-gcc41-mt-s -lpython2.6
Here is ldd for my shared object:
linux-gate.so.1 => (0x00327000)
libz.so.1 => /lib/libz.so.1 (0x004f4000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00110000)
libm.so.6 => /lib/libm.so.6 (0x00f31000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x0053b000)
libc.so.6 => /lib/libc.so.6 (0x00328000)
/lib/ld-linux.so.2 (0x0077d000)
I will try with -fno-inline flag, but I was wondering this would impact performance of compiled code.
I doubt that is because I link statically against python 2.6? There should be libpython2.6.so in my ldd, but I don't see it.
Are there something wrong in my link or compile flags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这些外部库是静态的,它们正在被合并到您的库中。我怀疑这是因为 ldd 没有显示到它们的链接,这是产生巨大可执行文件的常见原因。
g++ 中的 -static 链接器选项可能会导致此问题,或者您正在链接的库只能作为静态库使用。
It looks like those external libraries are static, they are being incorporated into your library. I suspect this because ldd is not showing a link to them and this is a frequent cause of huge executables.
The -static linker option in g++ can cause this problem, or maybe the libraries you are linking are only available as static libraries.