{"id":13773972,"url":"https://github.com/extism/cpp-sdk","last_synced_at":"2025-04-08T07:32:23.131Z","repository":{"id":195690803,"uuid":"691271862","full_name":"extism/cpp-sdk","owner":"extism","description":"Extism C++ Host SDK","archived":false,"fork":false,"pushed_at":"2024-05-22T20:57:03.000Z","size":2704,"stargazers_count":6,"open_issues_count":2,"forks_count":3,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-05-22T21:52:07.255Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/extism.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-13T20:57:31.000Z","updated_at":"2024-06-24T02:12:31.274Z","dependencies_parsed_at":"2024-01-15T02:29:54.770Z","dependency_job_id":"81a9047f-5165-4b9c-b521-0ec223373314","html_url":"https://github.com/extism/cpp-sdk","commit_stats":null,"previous_names":["extism/cpp-sdk"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fcpp-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fcpp-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fcpp-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fcpp-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/extism","download_url":"https://codeload.github.com/extism/cpp-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247796297,"owners_count":20997545,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T17:01:22.485Z","updated_at":"2025-04-08T07:32:21.897Z","avatar_url":"https://github.com/extism.png","language":"C++","funding_links":[],"categories":["\u003ca name=\"extism\"\u003e\u003c/a\u003e[Extism](https://github.com/extism/extism) \u003csup\u003e[top⇈](#contents)\u003c/sup\u003e"],"sub_categories":[],"readme":"# Extism cpp-sdk\n\nThe C++ SDK for integrating with the [Extism](https://extism.org/) runtime. Add this library in your host C++ applications to run Extism plugins.\n\nJoin the [Extism Discord](https://extism.org/discord) and chat with us!\n\n## Building and Installation\n\n### Install Dependencies\n\n- [libextism](https://extism.org/docs/install)\n- cmake and jsoncpp\n  - Debian: `sudo apt install cmake jsoncpp`\n  - macOS: `brew install cmake jsoncpp`\n\nIf you wish to do link jsoncpp statically, or do an in-tree build, See\n[Alternative Dependency Strategies](#Alternative-Dependency-Strategies).\n\n### Build and Install cpp-sdk\n\n```bash\ncmake -B build\ncmake --build build -j\nsudo cmake --install build\n```\n\n## Getting Started\n\nTo add the cpp-sdk to a CMake project:\n\n```cmake\nfind_package(extism-cpp)\ntarget_link_libraries(getting-started extism-cpp)\n```\n\n### Loading a Plug-in\n\nThe primary concept in Extism is the [plug-in](https://extism.org/docs/concepts/plug-in). A plug-in is a code module stored in a `.wasm` file.\n\nPlug-in code can come from a file on disk, object storage or any number of places. Since you may not have one handy, let's load a demo plug-in from the web. Let's\nstart by creating a main function that loads a plug-in:\n\n```cpp\n#include \u003cextism.hpp\u003e\n\nint main(void) {\n  const auto manifest =\n      extism::Manifest::wasmURL(\"https://github.com/extism/plugins/releases/\"\n                                \"latest/download/count_vowels.wasm\");\n  extism::Plugin plugin(manifest, true);\n}\n```\n\n### Calling A Plug-in's Exports\n\nThis plug-in was written in Rust and it does one thing, it counts vowels in a string. It exposes one \"export\" function: `count_vowels`. We can call exports using `Plugin::call`.\nLet's add code to call `count_vowels` to our main func:\n\n```c++\n#include \u003cextism.hpp\u003e\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n\nint main(void) {\n  // ...\n\n  const std::string hello(\"Hello, World!\");\n  auto out = plugin.call(\"count_vowels\", hello);\n  std::string response(out.string());\n  std::cout \u003c\u003c response \u003c\u003c std::endl;\n  // =\u003e {\"count\":3,\"total\":3,\"vowels\":\"aeiouAEIOU\"}\n}\n```\n\nBuild it:\n\n```bash\ncmake -B build \u0026\u0026 cmake --build build\n```\n\nRunning this should print out the JSON vowel count report:\n\n```shell\n./build/getting-started\n{\"count\":3,\"total\":3,\"vowels\":\"aeiouAEIOU\"}\n```\n\nAll exports have the same interface, optional bytes in and optional bytes out. This plug-in happens to take a string and return a JSON encoded string with a report of results.\n\n### Plug-in State\n\nPlug-ins may be stateful or stateless. Plug-ins can maintain state between calls using variables. Our count vowels plug-in remembers the total number of counted vowels in the \"total\" key in the result. You can see this by making subsequent calls to the export:\n\n```cpp\n  const std::string hello(\"Hello, World!\");\n  auto out = plugin.call(\"count_vowels\", hello);\n  std::string response(out.string());\n  std::cout \u003c\u003c response \u003c\u003c std::endl;\n  // =\u003e {\"count\":3,\"total\":3,\"vowels\":\"aeiouAEIOU\"}\n\n  out = plugin.call(\"count_vowels\", hello);\n  response = out.string();\n  std::cout \u003c\u003c response \u003c\u003c std::endl;\n  // =\u003e {\"count\":3,\"total\":6,\"vowels\":\"aeiouAEIOU\"}\n```\n\nThe state variables will persist until the plug-in is freed or reinitialized.\n\n### Configuration\n\nPlug-ins may optionally take a configuration object. This is a static way to configure the plug-in. Our count-vowels plugin takes an optional configuration to change out which characters are considered vowels. Example:\n\n```cpp\n#include \u003cextism.hpp\u003e\n\nint main(void) {\n  auto manifest =\n      extism::Manifest::wasmURL(\"https://github.com/extism/plugins/releases/\"\n                                \"latest/download/count_vowels.wasm\");\n  manifest.setConfig(\"vowels\", \"aeiouyAEIOUY\");\n  extism::Plugin plugin(manifest, true);\n\n  const std::string hello(\"Yellow, World!\");\n  auto out = plugin.call(\"count_vowels\", hello);\n  std::string response(out.string());\n  std::cout \u003c\u003c response \u003c\u003c std::endl;\n  // =\u003e {\"count\":4,\"total\":4,\"vowels\":\"aeiouyAEIOUY\"}\n}\n```\n\n\n### Host Functions\n\nLet's extend our count-vowels example a little bit: Instead of storing the `total` in an ephemeral plug-in var, let's store it in a persistent key-value store!\n\nWasm can't use our KV store on it's own. This is where [Host Functions](https://extism.org/docs/concepts/host-functions) come in.\n\n[Host functions](https://extism.org/docs/concepts/host-functions) allow us to grant new capabilities to our plug-ins from our application. In this case, they are native C++ functions that are passed to and can can be invoked by the plug-in.\n\nLet's load the manifest like usual but load up the `count_vowels_kvstore` plug-in:\n\n```cpp\n  const auto manifest =\n      extism::Manifest::wasmURL(\"https://github.com/extism/plugins/releases/\"\n                                \"latest/download/count_vowels_kvstore.wasm\");\n```\n\n\u003e *Note*: The source code for this is [here](https://github.com/extism/plugins/blob/main/count_vowels_kvstore/src/lib.rs) and is written in rust, but it could be written in any of our PDK languages.\n\nUnlike our previous plug-in, this plug-in expects you to provide host functions that satisfy its import interface for a KV store.\n\nWe want to expose two functions to our plugin, `kv_write` which writes a bytes value to a key and `kv_read` which reads the bytes at the given key:\n\n```cpp\n  // pretend this is Redis or something :)\n  std::map\u003cstd::string, std::vector\u003cuint8_t\u003e\u003e kvStore;\n\n  auto t = std::vector\u003cextism::ValType\u003e{extism::ValType::I64};\n  auto kvRead = extism::Function(\n      \"kv_read\", t, t,\n      [\u0026kvStore](extism::CurrentPlugin plugin, void *user_data) {\n        const auto it = kvStore.find(plugin.inputString());\n        if (it == kvStore.end()) {\n          const std::vector\u003cuint8_t\u003e zeros{0, 0, 0, 0};\n          plugin.output(zeros.data(), zeros.size());\n          return;\n        }\n        plugin.output(it-\u003esecond.data(), it-\u003esecond.size());\n      });\n  auto kvWrite = extism::Function(\n      \"kv_write\", {extism::ValType::I64, extism::ValType::I64}, {},\n      [\u0026kvStore](extism::CurrentPlugin plugin, void *user_data) {\n        const auto key = plugin.inputString(0);\n        auto value = plugin.inputBuffer(1);\n        kvStore[key] = value.vector();\n      });\n```\n\nWe need to pass these imports to the plug-in to create them. All imports of a plug-in must be satisfied for it to be initialized:\n\n```cpp\n  extism::Plugin plugin(manifest, true, {kvRead, kvWrite});\n```\n\nNow, we can demo it:\n\n```cpp\n  const std::string hello(\"Hello, World!\");\n  auto out = plugin.call(\"count_vowels\", hello);\n  std::string response(out.string());\n  std::cout \u003c\u003c response \u003c\u003c std::endl;\n  // =\u003e {\"count\":3,\"total\":3,\"vowels\":\"aeiouAEIOU\"}\n\n  out = plugin.call(\"count_vowels\", hello);\n  response = out.string();\n  std::cout \u003c\u003c response \u003c\u003c std::endl;\n  // =\u003e {\"count\":3,\"total\":6,\"vowels\":\"aeiouAEIOU\"}\n```\n\n## Linking\n\n#### CMake\n\n```cmake\nfind_package(extism-cpp)\ntarget_link_libraries(target_name extism-cpp)\n```\n\nor statically:\n\n```cmake\nfind_package(extism-cpp)\ntarget_link_libraries(target_name extism-cpp-static)\n```\n\n#### `pkg-config`\n\n```bash\npkg-config --libs extism-cpp\n```\n\nor statically:\n\n```bash\npkg-config --static --libs extism-cpp-static\n```\n\n## Alternative Dependency Strategies\n\n### link jsoncpp statically\n\nFor builds using `extism-cpp-static` to be static other than glibc, etc. jsoncpp\nmust be linked statically. jsoncpp provided by package managers often isn't\nprovided as a static library.\n\nBuilding and installing jsoncpp from source (including `jsoncpp_static`):\n\n```bash\ngit clone https://github.com/open-source-parsers/jsoncpp\ncd jsoncpp\ncmake -B build \u0026\u0026 cmake --build build -j\nmake -C build install\n```\n\n### In-Tree builds\n\nIf you wish, instead of using installed deps, you can do an in-tree build!\n\nFor example:\n\n```bash\nmkdir extism-cpp-project\ncd extism-cpp-project\ngit init\ngit submodule add https://github.com/extism/extism.git\ngit submodule add https://github.com/open-source-parsers/jsoncpp.git\ngit submodule add https://github.com/extism/cpp-sdk.git\ncd cpp-sdk\ncmake -DEXTISM_CPP_BUILD_IN_TREE=1 -B build \u0026\u0026 cmake --build build\n```\n\n### Fallback Dependencies\n\nIf `EXTISM_CPP_BUILD_IN_TREE` is not set and `find_package` fails, the dependency is fetched from the internet using `FetchContent` and built from source.\n\nThe author believes this is the worst way to deal with the deps as it requires building them from source and doesn't use a centrally managed, flat tree of dependencies. It's provided just as a cross-platform convenience.\n\n## Testing\n\n```bash\ncmake --build build --target test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextism%2Fcpp-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fextism%2Fcpp-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextism%2Fcpp-sdk/lists"}