简体   繁体   中英

Reduce Python static library size

I'm trying to build a minimal Python static library for distribution in an app. This is the C API I need to link to, I believe it may be called CPython as well. I don't need to package actual Python scripts, just link to the library itself.

I've built 3.7.4 by doing:

./configure --disable-shared
make

This does build a library libpython3.7m.a which is 12.4MB. Is it possible to reduce this size at all? I need an absolute barebones distribution without any of the normal packages. Literally this is just for a scripting bridge and doesn't need any of the usual Python functionality.

If you're trying to link to Python's Extension API , you won't find much luck in shaving off size from the libpython shared object. This is because the Extension API uses the same types and headers as the core interpreter (which is why it's called extensions instead of something else like plugins), granting the extensions runtime access to the inner mechanics of the interpreter that normal python scripts wouldn't allow. Basically, the libpython object is just the interpreter compiled without an entry point and an additional frontend so that the expression of simple python operations (eg parsing function arguments) stays a clean development experience.

If you're using Python 3.2+, there is offered a "Limited API" for the version of python you are trying to target, but the point of this is to guarantee a stable ABI as the extensions API evolves, not to reduce the size of the compilation unit.

I would also like to mention that the term CPython is just a way to disambiguate the vanilla Python distribution from similar but incompatible interpreters like Pypy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM