• Stars
    star
    946
  • Rank 48,319 (Top 1.0 %)
  • Language
    C
  • License
    The Unlicense
  • Created almost 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Include binary files in C/C++

incbin

Include binary and textual files in your C/C++ applications with ease

Example

    #include "incbin.h"

    INCBIN(Icon, "icon.png");

    // Reference in other translation units like this
    INCBIN_EXTERN(Icon);

    // NOTE: Don't forget to use `extern "C"` in case of writing C++ code

    // You may specify an optional type for the included data array as a first
    // additional argument to INCBIN, the macro is overloaded by arity. The
    // default type is `unsigned char`.
    INCBIN(MyType, IconTyped, "icon.png");

    // INCTXT is the same as INCBIN but it uses type `char` by default and 
    // implicitly adds a NUL-terminator byte to the included data, making it
    // safe to use as a string in C.
    INCTXT(Readme, "readme.md");

    // Reference in other translation units like this
    INCTXT_EXTERN(Readme);

    // NOTE: Since INCTXT adds a NUL-terminator byte, it's size is one byte
    // larger than that of INCBIN, so subtract one for string length.

    // The macros produce three global (or external) symbols of the form
    // <type> <prefix><data><name>[]
    // <type> <prefix><end><name>*
    // unsigned int <prefix><size><name>
    //
    // The <type> by default is unsigned char, unless optionally provided.
    // The <prefix> by default is "g", you can override it with INCBIN_PREFIX.
    // The <name> is the identifier you give to INCBIN or INCTXT.
    // The <data>, <end>, and <size> are tokens that depend on INCBIN_STYLE, by
    // default they're "Data", "End", and "Size", but they can be changed to
    // "_data", "_end", and "_size" if INCBIN_STYLE is set to INCBIN_STYLE_SNAKE.

Portability

Known to work on the following compilers

  • GCC
  • Clang
  • PathScale
  • Intel
  • Solaris & Sun Studio
  • Green Hills
  • SNC (ProDG)
  • Diab C++ (WindRiver)
  • XCode
  • ArmCC
  • RealView
  • ImageCraft
  • Stratus VOS C
  • TinyCC
  • cparser & libfirm
  • LCC
  • MSVC See MSVC below

If your compiler is not listed, as long as it supports GCC inline assembler, this should work.

MISRA

INCBIN can be used in MISRA C setting. However it should be independently checked due to its use of inline assembly to achieve what it does. Independent verification of the header has been done several times based on commit: 7e327a28ba5467c4202ec37874beca7084e4b08c

Alignment

The data included by this tool will be aligned on the architectures word boundary unless some variant of SIMD is detected, then it's aligned on a byte boundary that respects SIMD convention just incase your binary data may be used in vectorized code. The table of the alignments for SIMD this header recognizes is as follows:

SIMD Alignment
SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2 16
Neon, AltiVec 16
AVX, AVX2 32
AVX512 64

Prefix

By default, incbin.h emits symbols with a g prefix. This can be adjusted by defining INCBIN_PREFIX before including incbin.h with a desired prefix. For instance

    #define INCBIN_PREFIX g_
    #include "incbin.h"
    INCBIN(test, "test.txt");

    // This translation unit now has three symbols
    // const unsigned char g_<name><data>[];
    // const unsigned char *const g_<name><end>;
    // const unsigned int g_<name><size>;

You can also choose to have no prefix by defining the prefix with nothing, for example:

    #define INCBIN_PREFIX
    #include "incbin.h"
    INCBIN(test, "test.txt");

    // This translation unit now has three symbols
    // const unsigned char <name><data>[];
    // const unsigned char *const <name><end>;
    // const unsigned int <name><size>;

Style

By default, incbin.h emits symbols with CamelCase style. This can be adjusted by defining INCBIN_STYLE before including incbin.h to change the style. There are two possible styles to choose from

  • INCBIN_STYLE_CAMEL (CamelCase)
  • INCBIN_STYLE_SNAKE (snake_case)

For instance:

    #define INCBIN_STYLE INCBIN_STYLE_SNAKE
    #include "incbin.h"
    INCBIN(test, "test.txt");

    // This translation unit now has three symbols
    // const unsigned char <prefix><name>_data[];
    // const unsigned char *const <prefix><name>_end;
    // const unsigned int <prefix><name>_size;

Combining both the style and prefix allows for you to adjust incbin.h to suite your existing style and practices.

Overriding Linker Output section

By default, incbin.h emits into the read-only linker output section used on the detected platform. If you need to override this for whatever reason, you can manually specify the linker output section.

For example, to emit data into program memory for esp8266/Arduino:

#define INCBIN_OUTPUT_SECTION ".irom.text"
#include "incbin.h"
INCBIN(Foo, "foo.txt");
// The three symbols below will all go to ".irom.text"
// <type> <prefix><data><name>[]
// <type> <prefix><end><name>*
// unsigned int <prefix><size><name>

You may also override the output section for data, and size separately, this is useful for Harvard architectures where program memory cannot be directly read from the program without special instructions. With this you can chose to put the size variable in RAM rather than ROM. This can be done with the macros

// <prefix><data><name> and <prefix><end><name> goes into custom data section
#define INCBIN_OUTPUT_DATA_SECTION "..."

// <prefix><size><name> goes into custom size section.
#define INCBIN_OUTPUT_SIZE_SECTION "..."

Explanation

INCBIN is a macro which uses the inline assembler provided by almost all compilers to include binary files. It achieves this by utilizing the .incbin directive of the inline assembler. It then uses the assembler to calculate the size of the included binary and exports two global symbols that can be externally referenced in other translation units which contain the data and size of the included binary data respectively.

MSVC

Supporting MSVC is slightly harder as MSVC lacks an inline assembler which can include data. To support this we ship a tool which can process source files containing INCBIN macro usage and generate an external source file containing the data of all of them combined. This file is named data.c by default. Just include it into your build and use the incbin.h to reference data as needed. It's suggested you integrate this tool as part of your projects's pre-build events so that this can be automated. A more comprehensive list of options for this tool can be viewed by invoking the tool with -help

If you're using a custom prefix, be sure to specify the prefix on the command line with -p <prefix> so that everything matches up; similarly, if you're using a custom style, be sure to specify the style on the command line with -S <style> as well.

NOTE: MSVC currently does not support INCTXT or custom optional type on INCBIN. This will be changed in the future.

Miscellaneous

Documentation for the API is provided by the header using Doxygen notation. For licensing information see UNLICENSE.

More Repositories

1

moreram

Get more system memory
C
804
star
2

breaking_the_physical_limits_of_fonts

Breaking the physical limits of fonts
JavaScript
319
star
3

glsl-parser

A GLSL parser
C++
251
star
4

lambdapp

Anonymous functions in C
C
247
star
5

gmqcc

An Improved Quake C Compiler
C++
159
star
6

normals_revisited

revisiting a known normal transformation in computer graphics
149
star
7

codin

Odin to C compiler
C
141
star
8

fpinspect

Inspect floating point computations
C
135
star
9

cvec

No bullshit vector library for C
C
78
star
10

neothyne

Engine and game
C++
77
star
11

libintrusive

Intrusive data structures for C
C
55
star
12

NVFC

OpenSource tool for monitoring, configuring and overclocking NVIDIA GPUs
C
44
star
13

scope_stack_alloc

A scoped stack allocator
C++
36
star
14

deshade

dump and replace shaders of any OpenGL or Vulkan application
C++
29
star
15

gml

Dynamically typed, higher-order, semi-functional, interpreted and embeddable programming language
C
28
star
16

0xABAD1DEA

Static global objects with constructors and destructors made useful in C++
C++
27
star
17

fibers

The fiber sourcebook
HTML
21
star
18

smbf

Static model binary format
C++
19
star
19

gmrtdxt

Realtime DXT compressor and optimizer
C++
19
star
20

fpot

Fast Point Overlap Test
C
17
star
21

dep_sort

Generic topological sorting for sorting a list of dependencies in C++17
C++
13
star
22

wfstd

Standard library I developed while working for Wayforward
C
11
star
23

vector_benchmark

Benchmarking a trivial replacement for std::vector
C++
11
star
24

alice

A barebones kernel for i386
C
10
star
25

bbgl

OpenGL Rendering as a seperate process (Black Box)
C
10
star
26

redroid

The ultimate IRC bot
C
9
star
27

lua-vec

highly efficent, caching, copy-on-write lua vector math library
Lua
7
star
28

printer-display

Use your printer as a display
C
7
star
29

odin_review

A review of the Odin programming language
HTML
6
star
30

pastes

Just a place where I store my pastes
C
6
star
31

discord-rogue

A tiny rogue like for Discord on nodejs
JavaScript
6
star
32

pds2tc

Public domain S2TC implementation
C
5
star
33

aau

Almost Always Unsigned
HTML
5
star
34

Kaizen

a small, embeddable continous integration framework for small projects
C
4
star
35

libpartial

Partially applied functions for C
C
4
star
36

xcpumemperf

Benchmark to determine cross CPU memory performance for UNIX/POSIX systems
C
3
star
37

zbar-lite

Stripped down light weight version of the zbar library
C
3
star
38

aoc

advent of code 2018 solutions https://adventofcode.com/
C++
3
star
39

glsl-compiler

GLSL compiler targeting a simple SSA IR
3
star
40

smm_video_scraper

Scrape level codes from SMM videos
Python
3
star
41

nra

patch games for nes mini and snes mini to use retroarch cores automatically
C
2
star
42

windows_explicide_runtime_imports

Explicit runtime imports for Windows
C
1
star
43

CV

CV, resume of @graphitemaster
1
star