A simple tool for embedding files through static arrays, written in C89.
- C23's
#embed
is cool and may even be supported by a small number of compilers in a decade or two. ld
magic is not available on Windows.xxd
is an external tool that you have to install,semb
builds and runs from CMake.
- You don't need to embed files.
- CMake 3.20 is required for
cmake_path
command. - Big files are slow to embed and even slower to compile.
- Having ~140 LOC dependency is a bit too much for you.
- Add this repo as a CMake subdirectory (you only need
semb.c
andCMakeLists.txt
); - Use CMake function
semb_generate
; #include
generated file and use embedded data.
semb_generate(<target> OUT <output.h> [BINARY] FILES <file1> [<fileN>...])
What it does:
- adds current binary directory as include directory for
target
; - generates
output.h
in current binary directory during build process:- any relative path may be used, e.g.
../foo/bar.h
; - to
#include
the generated file simply use the same path;
- any relative path may be used, e.g.
- adds
output.h
to the source list oftarget
. fileN
are paths of files to be embedded:- if
BINARY
option is set, paths are resolved from current binary directory; - if
BINARY
option is not set, paths are resolved from current source directory.
- if
semb_generate(my-target OUT embed.h FILES
../img/file1.png
doc/file2.txt
file3.ext)
Will generate:
static const unsigned char file1_png[] = {
0x00, 0x00, ...
};
static const unsigned char file2_txt[] = {
0x00, 0x00, ...
};
static const unsigned char file3_ext[] = {
0x00, 0x00, ...
};
Let's make a program that prints its own source code.
.
├─ external
│ └─ semb
│ ├─ CMakeLists.txt
│ └─ semb.c
├─ CMakeLists.txt
└─ main.c
cmake_minimum_required(VERSION 3.20)
project(example C)
add_subdirectory(external/semb)
add_executable(example main.c)
semb_generate(example OUT embed_main.h FILES main.c)
#include <stdio.h>
#include "embed_main.h"
int main() {
for (int i = 0; i < sizeof(main_c); i++) {
printf("%c", (char)main_c[i]);
}
return 0;
}