Embed resources into binary with XXD and CMake in a cross-platform way (Linux, Windows and Mac support).
XXD converts the data file into a C array, which is slow to compile for large files, due to the way how compiler represents the array internally. In order to embed large files, please use res_embed instead.
- Add this xxd project to your CMake project as a submodule:
cd some/path
git submodule add https://github.com/dmikushin/xxd.git
- Integrate xxd project into your
CMakeLists.txt
:
add_subdirectory(some/path/xxd)
- Embed the following example resource file into an executable using
xxd_embed
macro:
$ cat resource
Hello, world!
set(SRCS "example.cpp")
xxd_embed("resource", ${CMAKE_CURRENT_SOURCE_DIR}/resource SRCS)
add_executable(example ${SRCS})
target_link_libraries(example xxd::xxd)
- Use the embedded resource in your program:
#include "xxd.h"
#include <cstdio>
int main(int argc, char* argv[])
{
printf("%s", xxd::get("resource"));
return 0;
}
This code uses the original public domain xxd
utility, which is
(c) 1990-1998 by Juergen Weigert