开发者

How to reduce file size of g++ compiled shared object?

开发者 https://www.devze.com 2023-04-10 16:23 出处:网络
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.

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开发者_如何学JAVA 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"$@" "$<"

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消