The size of the generated library significantly increased after referencing -llog in the C++ code

55 Views Asked by At

I compiled my C++ code into a dynamic library using the NDK. Initially, my code was linked with the log4 dynamic library and used -llog to include the Android logging library. However, later on, I removed the code references to log4 and only kept -llog. Surprisingly, the size of my dynamic library increased significantly compared to the previous build, and it introduced many symbols related to itanium_demangle. Why did this happen?

The -Os command did not have any effect, as observed when using the nm command, which revealed the presence of numerous itanium_demangle symbols. Analyzing the size with the size command showed a significant increase in debuginfo data.

Update:

Employed Russian is correct. I compared two approaches and obtained the following results:

liblog4cplus.so: shared definition of __cxa_demangle
/aarch64-linux-android/libc++abi.a: lazy definition of __cxa_demangle
/aarch64-linux-android/libc++abi.a(cxa_default_handlers.o): reference to __cxa_demangle
/aarch64-linux-android/libc++abi.a(cxa_demangle.o): definition of __cxa_demangle 

From the above, it can be seen that when linking the log4 library, the functionality of __cxa_demangle is already included in the log4 library, so it will not be included in the final generated library. However, when removing the log4 linkage, the functionality of __cxa_demangle needs to be directly linked into the generated library, resulting in an increase in library size.

1

There are 1 best solutions below

7
On

Why did this happen?

(Likely) because the symbols were previously provided by the log4 shared library, but now remain unresolved and thus pulled in from libc++.a (or whatever C++ library you are using).

You can see where the symbol is referenced and where it is defined using -Wl,-y,__cxa_demangle on the link line. Use this command with the old and the new setup, and you should see the difference.

You should also read this post to understand how linker works with archive libraries.