DEV Community

HarmonyOS
HarmonyOS

Posted on

Handling Abnormally Large .so Files After Modifying Build Configuration Parameters

Read the original article:Handling Abnormally Large .so Files After Modifying Build Configuration Parameters

Problem Description

After adding the parameter "arguments": "-v -DOHOS_STL=c++_static" in the module-level build configuration, the .so file size increased abnormally instead of decreasing.
Originally, this modification was intended to remove the dependency on libc++_shared.so, but it unexpectedly caused the .so file to grow significantly.

cke_3596.png

Background Knowledge

Shared Object (.so) Linking Methods:

  • Static Linking:
    • The library code is compiled directly into the executable or .so file.
    • Use the -static option or specify the absolute path of the library file during compilation.
    • Advantages: The resulting binary is self-contained and does not depend on external libraries.
    • Disadvantages: Each binary includes its own copy of the library, resulting in a much larger .so file and wasted storage space.
  • Dynamic Linking:
    • The program dynamically loads .so libraries at runtime using functions such as dlopen().
    • Advantages: Enables flexible runtime loading, reduces binary size, and allows shared usage across applications.
    • Disadvantages: Requires managing library dependencies and ensuring library paths are correct.
  • Symbol Table in .so Files:
    • Records information about functions and variables in the shared library (e.g., names, addresses, and sizes).
    • Useful for debugging and crash analysis.
    • Stripping symbol tables can significantly reduce .so file size.

Analysis Conclusion

When -DOHOS_STL=c++_static is used, the standard C++ library (libc++) is linked statically into your .so file.
This means all the standard library symbols are compiled directly into the .so, leading to a large size increase.
The original shared linking mode (c++_shared) only referenced the shared library dynamically, keeping the .so smaller.

Solution

To reduce .so file size after static linking changes, apply the following optimizations:

1.Set Build Type to Release
Remove debugging information by adding:

   -DCMAKE_BUILD_TYPE=Release
Enter fullscreen mode Exit fullscreen mode

2.Strip Symbol Table
Remove symbol table information to reduce file size by adding:

   cppFlags: "-s"
Enter fullscreen mode Exit fullscreen mode

3.Use Shared Linking Instead of Static Linking
Revert to using shared linking if not strictly required:

   -DOHOS_STL=c++_shared
Enter fullscreen mode Exit fullscreen mode

4.Enable Compiler Optimization for Size
Use -Os or -Oz to optimize for smaller binaries:

   cppFlags: "-Os"
   // or
   cppFlags: "-Oz"
Enter fullscreen mode Exit fullscreen mode

Recommended Configuration Example

"externalNativeOptions": {
  "path": "./src/main/cpp/CMakeLists.txt",
  "arguments": "-DOHOS_STL=c++_shared -DCMAKE_BUILD_TYPE=Release",
  "cppFlags": "-s -Os"
}
Enter fullscreen mode Exit fullscreen mode

This configuration:

  • Builds in Release mode.
  • Strips debugging and symbol information.
  • Uses shared STL linking to prevent library duplication.
  • Applies size optimization for minimal .so output.

Verification Result

  • Not applicable — this issue concerns build configuration and file size optimization rather than runtime behavior.
  • Applicable to NDK-based HarmonyOS native module builds.
  • Compatible with Release build types only.
  • Must recompile the project after modifying CMake arguments for changes to take effect.

Related Documents or Links

https://developer.huawei.com/consumer/en/doc/harmonyos-guides/build-with-ndk-cmake

Written by Arif Emre Ankara

Top comments (0)