You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Generate `file.c` and `file.h` from binary contents of `file.txt`:# `const unsigned char *file`: file contents as a byte string# `const size_t file_size`: file size
python file2c.py file.txt -o file.c
python file2c.py file.txt -o file.h --header
# Generate `file.c` and `file.h` from text contents of `file.txt`:# `const char *file`: file contents as a null-terminated char string# `const size_t file_size`: file size
python file2c.py file.txt --text -o file.c
python file2c.py file.txt --text -o file.h --header
# Generate `file.c` and `file.h` from binary contents of `file.txt`,# customizing the variable symbol name to "contents":# `const unsigned char *contents`: file contents as a byte string# `const size_t contents_size`: file size
python file2c.py file.txt --symbol contents -o file.c
python file2c.py file.txt --symbol contents -o file.h --header
Integrating with CMake
In CMake:
# 1. Include file2c.cmake scriptinclude(path/to/file2c.cmake)
# 2. Generate C libraryadd_file2c(
new_library_targetINPUTinput_file_to_be_embedded# Optional: choose the name of the global variable/header file# Defaults to input file nameSYMBOLsome_c_symbol# Pass "TEXT" to embed contents as text, omit for binary contentsTEXT
)
# 3. Link the generated library with other targetstarget_link_libraries(my_existing_targetnew_library_target)
In C/C++:
// 1. Include generated header#include<some_c_symbol.h>// 2. Use the embedded contentsvoidprint_contents() {
printf("Content: %s\n", some_c_symbol);
printf("Size: %lu\n", some_c_symbol_size);
}
About
Python script that generates C source files with global variables embedding binary/text file contents, with easy integration for CMake projects