Code Monkey home page Code Monkey logo

cmake-embed's Introduction

CMake embed

CMake script for embedding resources in binaries.

Resources are embedded in an object library target, and can be accessed via functions in a generated header.

For the script to work, the xxd program must be available to CMake.

Requires CMake >= 3.17.

Example usage:

CMakeLists.txt

include(EmbedResources)

add_embedded_binary_resources(
  foo_resources_binary
  OUT_DIR foo/resources
  HEADER binary.hpp
  NAMESPACE foo::resources
  RESOURCE_NAMES resource_a resource_b
  RESOURCES 
  "${PROJECT_SOURCE_DIR}/data/resource_a.bin"
  "${PROJECT_SOURCE_DIR}/data/resource_b.bin"
)

add_embedded_text_resources(
  foo_resources_text
  OUT_DIR foo/resources
  HEADER text.hpp
  NAMESPACE foo::resources
  RESOURCE_NAMES resource_c
  RESOURCES 
  "${PROJECT_SOURCE_DIR}/data/resource_c.txt"
)

add_executable(foo)
target_sources(foo PRIVATE foo.cpp)
target_link_libraries(
    foo PRIVATE 
    foo_resources_binary foo_resources_text
)

foo.cpp

#include <foo/resources/binary.hpp>
#include <foo/resources/text.hpp>

auto main() -> int 
{
    std::span<std::byte const> resource_a = foo::resources::resource_a();
    std::span<std::byte const> resource_b = foo::resources::resource_b();

    std::string_view resource_c = foo::resources::resource_c();

    // ... use the resources ...
}

Using with older C++ standards

You can override the span, byte and string_view types in the generated files to be from a library with a compatible interface instead of std (e.g. span-lite, Microsoft.GSL). See the documentation in the EmbedResources.cmake script.

cmake-embed's People

Contributors

miso1289 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.