1

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"$@" "$<"

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?

Bear
  • 1,367
  • 2
  • 13
  • 19
  • Oh, I made a mistake, I tried -Os, this results 5MB, my original post said 5MB is compiled with -O1, that's wrong. I fixed the post. – Bear Oct 05 '11 at 14:23
  • That's all I got then, sorry. – Blindy Oct 05 '11 at 14:25
  • Please post `ldd $target.so` and try -fno-inline etc. Are you using any special template features (MSVC++ extern templates e.g.?) – sehe Oct 05 '11 at 14:34
  • I don't use any special template features, all the syntax I used are follow the standard. If there are any fancy features I used, it might be Boost library, like Boost::MPL, Boost::Tuple, Boost::Variant etc. – Bear Oct 05 '11 at 14:49

1 Answers1

1

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.

teambob
  • 1,994
  • 14
  • 19