Resource Embedding in C++

Published November 16, 2025 · 3 min read

Embedding resources directly into a C++ binary is a practical way to improve performance, enhance startup reliability, and make small tools more portable. In this project, I present a minimal, reproducible approach: converting any text file into a C header with a byte array at build time, compiling that header into the executable, and managing the build tools and dependencies with Conan 2. The following notes explain why you might choose this pattern, how it maps to the rest of this proof-of-concept, and what trade-offs to consider.

Overview

Why embed a resource?

How this proof-of-concept works (high level)

Implementation Example

Conan

Technically, this approach does not rely on Conan. However, Conan can be beneficial if the tools you use are managed by it. For simplicity, this example omits such integration and uses Conan primarily as a wrapper around CMakeLists.txt, given its popularity as a package manager.

Here is a minimal Conan 2 configuration:

1from conan import ConanFile
2from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
3
4class SchemaPocConan(ConanFile):
5 name = "schema_poc"
6 version = "1.0"
7 settings = "os", "compiler", "build_type", "arch"
8 generators = "CMakeDeps", "CMakeToolchain"
9 exports_sources = "src/*", "CMakeLists.txt"
10
11 def layout(self):
12 cmake_layout(self)
13
14 def build(self):
15 cmake = CMake(self)
16 cmake.configure()
17 cmake.build()

Good ol' CMake

The idea is to generate a header file (here schema.hpp), that is generated at build time and compiled into the binary to provide a SCHEMA bute array, which we can then use in our code.

1cmake_minimum_required(VERSION 3.15)
2project(schema_poc)
3
4add_executable(schema_poc src/main.cpp)
5
6add_custom_command(
7 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/schema.hpp
8 COMMAND xxd -n "SCHEMA" -i ${CMAKE_CURRENT_SOURCE_DIR}/src/schema.txt > ${CMAKE_CURRENT_BINARY_DIR}/schema.hpp
9 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/schema.txt
10)
11
12target_include_directories(schema_poc PRIVATE ${CMAKE_BINARY_DIR})
13target_sources(schema_poc PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/schema.hpp)

This is a minimal example of a CMakeLists.txt that generates an executable. Notice the add_custom_command, which is where all the magic really happens:

How to use it in C++

How you use your assets is up to you.

In my case, schema.txt contains a single ascii word. In the following program, we grab the first argument and check wether it is equal to the word provided by the schema.

1#include <iostream>
2#include <cstring>
3
4#include "schema.hpp"
5
6int main(int argc, char** argv) {
7 if (argc > 1 && strcmp(argv[1], reinterpret_cast<const char*>(SCHEMA)) == 0) {
8 std::cout << "equal" << std::endl;
9 return 0;
10 }
11
12 std::cerr << "not equal" << std::endl;
13 return 1;
14}

Limitations

While the tool I used (xxd) is very handy for such tasks, yet it is not a Conan package at the moment. So integration with certian CI workflows or cross-compilation environments might become tricky.

An alternative would be to manually have a script that does effectively the same, which can than be tracked as part of the project or integrated with Conan as a dependency.