top of page
  • Linkedin

General Discussions

Public·2 members

Ways to embed files in executables

Last year while working on a personal project, I needed to find a way to embed lua scripts inside

a C program. After searching the internet, I found several approaches to this problem.


C23 introduced a new pre-processor directive #embed it provides a standard way to include data by using

the pre-processor. However, the program I am working on is using a much older standard of the language (mainly for portability reasons).


Another technique is using the xxd utility, which provides the -i option that generates a C array of unsigned chars from a supplied file

echo hello! > testing.txt
xxd -i testing.txt testing.h
cat testing.h

Produces this

unsigned char testing_txt[] = {
	0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x0a
};
unsigned int testing_txt_len = 7;

A similar result can be obtained with a custom Python script.


It's a valid method that is useful for cases when a single file needs to be included, one significant limitation with this approach however, is that some editors or IDEs might not digest large C arrays when the file is opened, a freeze might occur. One has to declare the array using the extern storage identifier for it to be accessible for each element. Moreover, checking in large C array into version control, makes it slower to clone the project.


I've finally decided to go with objcopy which is a very practical tool included with most compiler toolchains, it can convert any file of any size into an object file and defines <name>_start and <name>_end symbols


Then by using the nm utility the name of the start/end symbols can be obtained from the produced object file and an 'array of references' can be constructed. I've wrote a simple shell script to achieve this - it generates a resources.h

header file that defines an array containing all resources.


https://github.com/etag4048/binembed


14 Views
bottom of page