Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add basic example for cpp binding #3108

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}