From d2e923cf628ebcd16562df6ff1442eae393f9078 Mon Sep 17 00:00:00 2001 From: Mingzhuo Yin Date: Mon, 18 Sep 2023 13:37:28 +0800 Subject: [PATCH] docs: add basic example for cpp binding (#3108) * chore: add basic example for cpp binding Signed-off-by: silver-ymz * typo Signed-off-by: silver-ymz * typo Signed-off-by: silver-ymz * use exist commit Signed-off-by: silver-ymz --------- Signed-off-by: silver-ymz --- examples/README.md | 1 + examples/cpp/CMakeLists.txt | 21 +++++++++++++++++++++ examples/cpp/README.md | 13 +++++++++++++ examples/cpp/basic.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 examples/cpp/CMakeLists.txt create mode 100644 examples/cpp/README.md create mode 100644 examples/cpp/basic.cpp diff --git a/examples/README.md b/examples/README.md index f1c0b202f475..a072e882b92b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,3 +7,4 @@ The goal of these documents is to provide you with everything you need to start ## Examples - [Rust](rust/README.md) +- [C++](cpp/README.md) \ No newline at end of file diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt new file mode 100644 index 000000000000..ba6948e295c6 --- /dev/null +++ b/examples/cpp/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.10) +project(opendal-cpp-examples) + +set(CMAKE_CXX_STANDARD 17) + +include(FetchContent) +FetchContent_Declare( + opendal-cpp + GIT_REPOSITORY https://github.com/apache/incubator-opendal.git + GIT_TAG 4b02228f7fe9a7c0f21a1660fee95716910c7a0a + SOURCE_SUBDIR bindings/cpp +) +FetchContent_MakeAvailable(opendal-cpp) + +add_executable(basic-example basic.cpp) +target_link_libraries(basic-example opendal_cpp) +include_directories(basic-example opendal_cpp) + +if(APPLE) + target_link_libraries(basic-example "-framework CoreFoundation -framework Security") +endif() \ No newline at end of file diff --git a/examples/cpp/README.md b/examples/cpp/README.md new file mode 100644 index 000000000000..2dfdcdad72a9 --- /dev/null +++ b/examples/cpp/README.md @@ -0,0 +1,13 @@ +# OpenDAL Cpp Examples + +Thank you for using OpenDAL Cpp Core! + +## Run Examples + +```bash +mkdir build +cd build +cmake .. +make +./basic-example +``` \ No newline at end of file diff --git a/examples/cpp/basic.cpp b/examples/cpp/basic.cpp new file mode 100644 index 000000000000..2ebb1504f97c --- /dev/null +++ b/examples/cpp/basic.cpp @@ -0,0 +1,25 @@ +#include "opendal.hpp" + +#include +#include +#include + +int main() { + char s[] = "memory"; + std::vector data = {'a', 'b', 'c'}; + + // Init operator + opendal::Operator op = opendal::Operator(s); + + // Write data to operator + op.write("test", data); + + // Read data from operator + auto res = op.read("test"); // res == data + + // Using reader + auto reader = op.reader("test"); + opendal::ReaderStream stream(reader); + std::string res2; + stream >> res2; // res2 == "abc" +} \ No newline at end of file