This is an automated email from the ASF dual-hosted git repository.

silver pushed a commit to branch cpp-example
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git

commit f8f1e2f51be3b4fc1c2b5b911dee71b1619299ca
Author: silver-ymz <[email protected]>
AuthorDate: Mon Sep 18 12:48:08 2023 +0800

    chore: add basic example for cpp binding
    
    Signed-off-by: silver-ymz <[email protected]>
---
 examples/README.md          |  1 +
 examples/cpp/CMakeLists.txt | 20 ++++++++++++++++++++
 examples/cpp/README.md      | 13 +++++++++++++
 examples/cpp/basic.cpp      | 25 +++++++++++++++++++++++++
 4 files changed, 59 insertions(+)

diff --git a/examples/README.md b/examples/README.md
index f1c0b202f..a072e882b 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 000000000..9d1afb72a
--- /dev/null
+++ b/examples/cpp/CMakeLists.txt
@@ -0,0 +1,20 @@
+cmake_minimum_required(VERSION 3.10)
+project(opendal-cpp-examples)
+
+set(CMAKE_CXX_STANDARD 17)
+
+include(FetchContent)
+FetchContent_Declare(
+    opendal-cpp
+    URL     
https://github.com/apache/incubator-opendal/releases/download/v0.40.0/opendal-cpp-v0.40.0.tar.gz
+    URL_HASH SHA256=<newest-tag-sha256>
+)
+FetchContent_MakeAvailable(opendal-cpp)
+
+add_executable(basic-exmaple basic.cpp)
+target_link_libraries(basic-exmaple opendal_cpp)
+include_directories(basic-exmaple opendal_cpp)
+
+if(APPLE)
+    target_link_libraries(opendal-cpp-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 000000000..acb9c47df
--- /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-exmaple
+```
\ No newline at end of file
diff --git a/examples/cpp/basic.cpp b/examples/cpp/basic.cpp
new file mode 100644
index 000000000..2ebb1504f
--- /dev/null
+++ b/examples/cpp/basic.cpp
@@ -0,0 +1,25 @@
+#include "opendal.hpp"
+
+#include <string>
+#include <vector>
+#include <iostream>
+
+int main() {
+  char s[] = "memory";
+  std::vector<uint8_t> 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

Reply via email to