-
January 27th, 2006, 12:17 AM
#1
C++/GCC Embedding binary data in executable
Hey, how would I go about embedding and using binary data from a file, an image perhaps, into an executable using C++ compiled with GCC? (well MinGW, but I’d like to compile for non-windows platforms as well) I would like to keep my game as a single executable file.
Sorry for the short, not so informative post but I don’t have a clue how to do this and therefore I’m not to sure what information I need to provide, if you need any more just ask, thanks.
Last edited by TdIclo; January 27th, 2006 at 12:22 AM.
-
January 27th, 2006, 01:25 AM
#2
Re: C++/GCC Embedding binary data in executable
Are you familiar with creating resources, like dialog boxes, bitmaps, etc.? Could you use that technique for what you're trying to do? I guess that depends partly on what platforms you're targeting.
-
January 27th, 2006, 01:34 AM
#3
Re: C++/GCC Embedding binary data in executable
Originally Posted by microcode
Are you familiar with creating resources, like dialog boxes, bitmaps, etc.? Could you use that technique for what you're trying to do? I guess that depends partly on what platforms you're targeting.
I’ve heard about them but never used, they seam to be a Windows/VC++ thing however and I do need to build for other platforms as well.
-
January 27th, 2006, 05:42 AM
#4
Re: C++/GCC Embedding binary data in executable
You can translate your binary file to C-code like:
Code:
(echo "const unsigned char binary_data[] = {"; od -txC myfile.bin | sed -e "s/^[0-9]*//" -e s"/ \([0-9a-f][0-9a-f]\)/0x\1,/g" -e"\$d" | sed -e"\$s/,$/};/") >myfile.c
Ugly but effective
-
January 27th, 2006, 01:44 PM
#5
Re: C++/GCC Embedding binary data in executable
Resources aren't just a Windows thing; other platforms use them too. It's worth looking into.
But if that's not an option, you can use the method mentioned by treuss above and convert the binary file to a C/C++ source file. I've used this technique often in embedded system projects, except that I wrote a program to do the conversion rather than using a cryptic command line.
The program simpy reads the binary file and generates a source file that defines a byte array with the file's data. Like so:
Code:
unsigned char raw_image_data[] =
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
// blah, blah, blah....lots of data bytes
};
const unsigned long raw_image_size = sizeof( raw_image_data ) / sizeof( raw_image_data[0] );
You can change your makefile so that if the image file changes, the utility program is called to automatically renegerate the source file.
Some targets limit the size of a statically-allocated array to 64Kb, so if your image file is larger than that, you'll have to generate multiple arrays and ensure that they are contiguous.
-
May 25th, 2009, 08:30 AM
#6
Re: C++/GCC Embedding binary data in executable
Originally Posted by treuss
You can translate your binary file to C-code like:
Code:
(echo "const unsigned char binary_data[] = {"; od -txC myfile.bin | sed -e "s/^[0-9]*//" -e s"/ \([0-9a-f][0-9a-f]\)/0x\1,/g" -e"\$d" | sed -e"\$s/,$/};/") >myfile.c
Ugly but effective
I believe I found a bug in that line of complex o.0 commands.
it should be
Code:
od -txC -v myfile.bin
with the -v, or else it will display an asterisk if the line above repeats. It's supposed to make it easier to read.
Please correct me if I'm wrong.
-
May 25th, 2009, 09:56 AM
#7
Re: C++/GCC Embedding binary data in executable
Congratulations. You dug out a thread which is over 3 years old
- Guido
-
August 9th, 2012, 07:49 PM
#8
Re: C++/GCC Embedding binary data in executable
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|