Skip to content

Commit

Permalink
docs: add basic example for cpp binding (apache#3108)
Browse files Browse the repository at this point in the history
* chore: add basic example for cpp binding

Signed-off-by: silver-ymz <yinmingzhuo@gmail.com>

* typo

Signed-off-by: silver-ymz <yinmingzhuo@gmail.com>

* typo

Signed-off-by: silver-ymz <yinmingzhuo@gmail.com>

* use exist commit

Signed-off-by: silver-ymz <yinmingzhuo@gmail.com>

---------

Signed-off-by: silver-ymz <yinmingzhuo@gmail.com>
  • Loading branch information
silver-ymz authored and Young-Flash committed Sep 19, 2023
1 parent 695ba5d commit e627f50
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Expand Up @@ -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)
21 changes: 21 additions & 0 deletions 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()
13 changes: 13 additions & 0 deletions 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
```
25 changes: 25 additions & 0 deletions 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"
}

0 comments on commit e627f50

Please sign in to comment.