bin2hpp
One day we'll get #embed and std::embed, but today is not that day.
CLI tool for converting files into header files which one can use to directly embed data in their C++ projects.
Building
cargo build
, or cargo [build | run | install] --profile optimised
to build an optimised binary.
The performance (but potentially not the executable size) can be further optimised by telling the compiler to use a more modern instruction set: RUSTFLAGS="-C target-cpu=native"
.
Usage
Basic usage
$ bin2hpp -i my_library.dll -o c_header.h
will generate a c_header.h
file in your current working directory containing something similar to the following:
// Generated by bin2hpp 0.3.0
#ifdef MY_LIBRARY_DLL_INIT
#define MY_LIBRARY_DLL_LEN 9728
const unsigned char MY_LIBRARY_DLL[MY_LIBRARY_DLL_LEN] = {77,90,144,0,3,0,0,0,4, ...};
#else
#define MY_LIBRARY_DLL_LEN 9728
extern const unsigned char MY_LIBRARY_DLL[MY_LIBRARY_DLL_LEN];
#endif
Note how in C mode one must #define MY_LIBRARY_DLL_INIT
, or whatever the corresponding implementation macro is for your generated header file, in exactly one C source file (*.c
, not in a *.h
file).
Example:
main.c
// In any other C source files other than the first: do not redefine
#define MY_LIBRARY_DLL_INIT
#include "c_header.h"
int main() {
return MY_LIBRARY_DLL_LEN;
}
Alternatively, in C++ mode: bin2hpp -i my_library.dll -o cpp_header.hpp --cpp --constexpr --stdarray --i8
// Generated by bin2hpp 0.3.0
#if !defined(CPP_HEADER_HPP)
#define CPP_HEADER_HPP
#include <cstdint>
#include <array>
#define MY_LIBRARY_DLL_LEN 9728
inline constexpr std::array<std::int8_t,MY_LIBRARY_DLL_LEN> MY_LIBRARY_DLL = {77,90,-112,0,3,0,0,0,4, ...};
#endif
Note about CLI arguments
Command line arguments are not positional. The input file path argument is the
only required command line argument. The command line argument parser will
choose the first instance of any provided argument. For example, if you provide
the -i
argument twice; only the first -i ./file/path
will be used. This behaviour
should not be relied upon as the implementation of the command line argument
parser may change at any time.