This isn't exactly easy to do. At least not portably.
On Windows, you can use resource files (.rc) to embed files into the executable. You can then access them from the program as arrays.
I've read about ways of doing this for UNIX, but to be honest, I've never gotten it to work.
One way of doing it that works everywhere is to take the file and convert it to a source file. For example:
uchar some_file[]={0x00,0xA0,0x25,/*...*/};
This is pretty cumbersome for even small files, however.
I've seen a program that could actually embed files into itself after compilation. It probably involved some sort of hack which I wouldn't recommend. Possibly something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
char *my_size="<<my size>>:_unset_"; //(The terminator is the most significant byte.)
int main(int argc,char **argv){
if (my_size_is_unset(*argv)){
int64_t size=get_my_size(*argv); //(If 64-bit integers aren't available, just use 32-bit.)
write_my_size(*argv,size);
return 0;
}
int64_t original_size=get_my_size(),
current_size=get_my_size(*argv);
if (current_size>original_size){
//There are embedded files at the end.
}
}
| |
This method in particular requires that the OS doesn't lock for writing the executable image of a program while the program is running. If it does, the first if can be moved to a separate program.
In order for this to work at all, the executable format must not rely on the size of the executable to work.