tree: 4927766ebcf1cc74ff114ace81130c3409b2fc5b [search]
  1. include/
  2. src/
  3. tests/
  4. .clang-format
  5. .gitignore
  6. build.rs
  7. Cargo.toml
  8. CMakeLists.txt
  9. CONTRIBUTING.md
  10. DEPENDENCIES.md
  11. DEPENDENCIES.rust.tsv
  12. Doxyfile
  13. README.md
  14. test_runner.sh
bindings/cpp/README.md

Apache OpenDALâ„¢ C++ Binding

status: unreleased

A C++ binding for OpenDAL, providing access to S3, GCS, Azure Blob, HDFS, the local filesystem, and 50+ more services through one API.

Note This binding is experimental. The API may change without notice.

The C++ binding is available as source. Its Cargo.toml enables the OpenDAL blocking API, and the default build includes the memory service.

Useful Links

Integration via CMake

FetchContent

include(FetchContent)

FetchContent_Declare(
  opendal-cpp
  GIT_REPOSITORY https://github.com/apache/opendal.git
  GIT_TAG        v0.40.0
  SOURCE_SUBDIR  bindings/cpp
)
FetchContent_MakeAvailable(opendal-cpp)

target_link_libraries(your_target opendal_cpp)

Vendored source

mkdir third_party && cd third_party
git clone https://github.com/apache/opendal.git
cd opendal && git checkout v0.40.0
add_subdirectory(third_party/opendal/bindings/cpp)
target_link_libraries(your_target opendal_cpp)

Prerequisites

  • CMake ≥ 3.22
  • Clang, AppleClang, or MSVC with C++17 support for the sync API
  • Clang or AppleClang with C++20 support for the async API

On Windows, compile targets that link opendal_cpp with the release DLL runtime (/MD) in every configuration to match the Rust static library. In CMake, set CMAKE_MSVC_RUNTIME_LIBRARY to MultiThreadedDLL before creating those targets. When using a system GoogleTest package, use one built with the same runtime.

Build

mkdir build && cd build
cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ..
make

CMake options

OPENDAL_FEATURES takes a comma-separated list of Cargo features. Use the opendal/ prefix for service features listed in the core Cargo.toml. To add S3, rerun CMake from bindings/cpp/build:

cmake -DCMAKE_CXX_COMPILER=clang++ -DOPENDAL_FEATURES="opendal/services-s3" ..
cmake --build .

Exposing additional Rust layers requires C++ binding code.

OptionDefaultDescription
OPENDAL_DEVOFFEnable all development options at once
OPENDAL_FEATURES""Additional Cargo features
OPENDAL_ENABLE_ASYNCOFFEnable async API (requires C++20)
OPENDAL_ENABLE_TESTINGOFFBuild and enable tests
OPENDAL_ENABLE_DOCUMENTATIONOFFBuild Doxygen docs
OPENDAL_ENABLE_ADDRESS_SANITIZEROFFEnable address sanitizer

Example

#include "opendal.hpp"
#include <iostream>

int main() {
    // Construct an operator. The second argument is a config map.
    opendal::Operator op("memory");

    // Write, read, stat.
    op.Write("hello.txt", "Hello, OpenDAL!");
    std::string data = op.Read("hello.txt");
    std::cout << data << "\n";

    opendal::Metadata meta = op.Stat("hello.txt");
    std::cout << "size=" << meta.ContentLength() << "\n";
}

See the user guide for more examples including streaming reads, directory listing, real backends, and the async API.

Contributing

See CONTRIBUTING.md at the repository root.

License and Trademarks

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

Apache OpenDAL, OpenDAL, and Apache are either registered trademarks or trademarks of the Apache Software Foundation.