C++/GCC Embedding binary data in executable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C++/GCC Embedding binary data in executable

  1. #1
    Join Date
    Jan 2006
    Posts
    3

    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.

  2. #2
    Join Date
    May 2005
    Posts
    151

    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.

  3. #3
    Join Date
    Jan 2006
    Posts
    3

    Re: C++/GCC Embedding binary data in executable

    Quote 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.

  4. #4
    Join Date
    Jan 2004
    Location
    DŁsseldorf, Germany
    Posts
    2,401

    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

  5. #5
    Join Date
    May 2005
    Posts
    151

    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.

  6. #6
    Join Date
    Mar 2009
    Posts
    7

    Re: C++/GCC Embedding binary data in executable

    Quote Originally Posted by treuss View Post
    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.

  7. #7
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: C++/GCC Embedding binary data in executable

    Congratulations. You dug out a thread which is over 3 years old

  8. #8
    Join Date
    Aug 2012
    Posts
    1

    Talking Re: C++/GCC Embedding binary data in executable

    good catch

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center