:: horsing around with the C++ programming language


embd is a tool for embedding files in to C++ source code. It generates a source file containing the embedded data and a header file that gives you all the facilities you need to access the embedded files.

I don't recommend using embd to embed large amounts of data, but it is useful for little demos and such like where you'd like a single file for ease of use and distribution.

The source files that embd generates are not licensed in anyway. You are free to apply your own copyright and favorite license or release the files in to the public domain.

Recent activity


Clone the repository using mercurial:

> hg clone

Or get a zip file of the code.

Quick start

To create a C++ source file and a corresponding header that contains files:

embd contract.txt logo.jpg

This will generated embd.cpp and embd.hpp. By compiling and linking embd.cpp in to your code, you can access the files as follows:

embd::file f("contract.txt"); // may throw an embd::file_not_found exception
std::istream &in = f.istream();

std::string token;
while ((in >> token))
    std::cout << "token: " << token << '\n';

There are many configurable options to change file names, namespaces, paths to the embedded files and so on.

Further reading



[01/03/2011 at 21:03:16]

Hi, looks very useful. I was looking for something like that, but I was wondering if you have a similar tool that embeding file and strings but only on header files, so there is no need for linking. Sure it will work only for text files and have limitations on the characters you can store. But it can save the burden of defining all linebreaks and add the escape sequences. My goal is to store some long hardcoded LaTeX command (100s of lines) like this:

... 100 lines

internally I need to convert it to something like
... 100 lines
-- Thanks


[03/03/2011 at 23:11:55]

Hi Alf,

I have no such C++ tool I'm afraid, but you could always use Python:

# source could come from a file:
# source = open(sys.argv[1], 'rb').read()
# but we'll use some dummy data
source = r"""\makeatletter
... 100 lines """

lines = source.splitlines()
literal = '\n'.join('    "' + repr(line)[1:-1] + '\\n"' for line in lines)
content = 'std::string s(\n' + literal + '\n);\n'

print content

This might not be bullet-proof, but with a little extra work could get you what you want.

(required, hint)

Links can be added like [this one ->], to my homepage.
Phrases and blocks of code can be enclosed in {{{triple braces}}}.
Any HTML markup will be escaped.