Skip to content

Python script and CMake functionality to easily embed and maintain resource files into C++ executables and libraries.

License

Notifications You must be signed in to change notification settings

jahnf/CMake-Resources

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMake-Resources

Python script and CMake functionality to easily embed and maintain resource files into C++ executables and libraries.

Example Usage

For a ready to use example have a look into the ./example directory.

1. Configure resources in JSON file(s)

Configure resources you want to embed in a JSON file, example: resources.json

{
  "CRES": [
    {
      "prefix": "sources/",
      "files": [
        { "name": "example.cc" },
        { "name": "example.cc", "alias": "example-alias.cc" }
      ]
    },
    {
      "prefix": "other-data/",
      "files": [
        { "name": "CMakeLists.txt" },
        { "name": "resources.json" }
      ]
    }
  ]
}

2. Include module in your CMakeLists.txt and add resource library

Example:

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(Resources)

# Creates a library target `resources` that can be used by and linked against all other targets.
add_resources_library(myresources resources.json)

3. Link against the resource library

add_executable(example example.cc)
target_link_libraries(example PRIVATE myresources)

4. Include header and list or access embedded files

example.cc

#include <iostream>
#include <myresources/resources.h>

int main(int argc, char** argv)
{
  // List all embedded resources
  for (const auto& res : myresources::embeddedResources())
  {
    std::cout << " - " << res.prefix << res.name << ", size=" << res.size << std::endl;
  }

  // Find and access specific embedded resource file
  // via operator[]
  if (const auto res = myresources::embeddedResources()["sources/example.cc"])
  {
    std::cout << " - " << res->prefix << res->name << ", size=" << res->size << std::endl;
    // res->data: pointer to data array of size res->size
  }

  // via get_resource function
  if (const auto res = myresources::get_resource("sources/example.cc"))
  {
    std::cout << " - " << res->prefix << res->name << ", size=" << res->size << std::endl;
  }

  return 0;
}

Future Ideas/Todos

About

Python script and CMake functionality to easily embed and maintain resource files into C++ executables and libraries.

Resources

License

Stars

Watchers

Forks