Created
December 22, 2019 03:10
-
-
Save Fireforge/9b487ae449e9578058d9f02b2caa7804 to your computer and use it in GitHub Desktop.
C/C++ Embedded Resources using CMake: This is a short CMake function that copies a folder of data files into a single header file that can be used in an application as embedded resources. This is an easy, cross-platform way to include a bunch of text data into an executable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creates C resources file from files in given directory | |
function(create_resources dir output) | |
# Create empty output file | |
file(WRITE ${output} "") | |
# Collect input files | |
file(GLOB bins ${dir}/*) | |
# Iterate through input files | |
foreach(bin ${bins}) | |
# Get short filename | |
string(REGEX MATCH "([^/]+)$" filename ${bin}) | |
# Replace filename spaces & extension separator for C compatibility | |
string(REGEX REPLACE "\\.| |-" "_" filename ${filename}) | |
# Read hex data from file | |
file(READ ${bin} filedata HEX) | |
# Convert hex data for C compatibility | |
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata}) | |
# Append data to output file | |
file(APPEND ${output} "const char ${filename}[] = {${filedata}0x00};\nconst unsigned ${filename}_size = sizeof(${filename});\n") | |
endforeach() | |
endfunction() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment