Home > OS >  How to make a c header file smaller? The header file is generated with "xxd -i"
How to make a c header file smaller? The header file is generated with "xxd -i"

Time:02-24

We need to store some data as c header file, so that we can then include it in the build bundle and shipped with any applications that use it.

To do that we use

xxd -i data.png > data.h

This works well, but the data.h files is now as 6X large as the data.png file. That means, if the data.png is 4MB, the data.h would be 24MB.

May I ask if there is a way to compress the data.h file to a smaller size?

Thanks!

--- update ---

Thank you all for the suggestions! I think I could clarify the need here to provide more context!

  1. the ideal way for us to consume the file is we can open it as input stream like
std::ifstream is;
infile.open("data.png");
somefunc(is) // a api function that takes std::istream as input

p.s. the file is not png file but a scripted model, I use png as example because I find it as a more generic problem of "xxd -i"

  1. we didn't find a way to make it available as a file to be read, as the file system the codes actually searching would be in Android/iOS. (only files on the mobile system are available and the source codes would be zipped in the .so file)

  2. with the header file we can do something like

std::stringstream is;
is.write((char*)data_byte_array, data_byte_array_len)
somefunc(is)

The source codes would end up built as a lib.so. In our tests, A 70KB data.h would end up adding 45KB to the lib.so.

CodePudding user response:

May I ask if there is a way to compress the data.h file to a smaller size?

You can use any lossless compression algorithm. The gzip program is a common default choice on POSIX systems.

CodePudding user response:

You can compile and binary file into an object file using objcopy. See C/C with GCC: Statically add resource files to executable/library for more details

  • Related