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.
Background Knowledge
Shared Object (.so) Linking Methods:
-
Static Linking:
- The library code is compiled directly into the executable or
.sofile. - Use the
-staticoption 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
.sofile and wasted storage space.
- The library code is compiled directly into the executable or
-
Dynamic Linking:
- The program dynamically loads
.solibraries at runtime using functions such asdlopen(). - 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.
- The program dynamically loads
-
Symbol Table in
.soFiles:- 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
.sofile 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
2.Strip Symbol Table
Remove symbol table information to reduce file size by adding:
cppFlags: "-s"
3.Use Shared Linking Instead of Static Linking
Revert to using shared linking if not strictly required:
-DOHOS_STL=c++_shared
4.Enable Compiler Optimization for Size
Use -Os or -Oz to optimize for smaller binaries:
cppFlags: "-Os"
// or
cppFlags: "-Oz"
Recommended Configuration Example
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt",
"arguments": "-DOHOS_STL=c++_shared -DCMAKE_BUILD_TYPE=Release",
"cppFlags": "-s -Os"
}
This configuration:
- Builds in Release mode.
- Strips debugging and symbol information.
- Uses shared STL linking to prevent library duplication.
- Applies size optimization for minimal
.sooutput.
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

Top comments (0)