tree: d2f7c315d1b30373338fa8b09f0efda2ac651d00 [path history] [tgz]
  1. BUILD
  2. BUILD.standalone
  3. CMakeLists.txt
  4. main.cc
  5. MODULE.bazel.example
  6. README.md
  7. run.sh
  8. run_bazel.sh
examples/cpp/hello_world/README.md

Fory C++ Hello World (Serialization)

This example demonstrates how to use Fory's high-performance serialization for C++ objects.

Prerequisites

  • CMake 3.16 or higher (for CMake build)
  • Bazel 8+ (for Bazel build)
  • C++17 compatible compiler (GCC 7+, Clang 5+, MSVC 2017+)

Building with CMake

Option 1: Using FetchContent (Recommended)

The easiest way to use Fory is with CMake's FetchContent module:

include(FetchContent)
FetchContent_Declare(
    fory
    GIT_REPOSITORY https://github.com/apache/fory.git
    GIT_TAG        main
    SOURCE_SUBDIR  cpp
)
FetchContent_MakeAvailable(fory)

target_link_libraries(your_app PRIVATE fory::serialization)

Build and Run

cd examples/cpp/hello_world
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --parallel
./hello_world

Or use the provided script:

./run.sh

Building with Bazel

In-repo build (for testing within Fory repository)

From the repository root:

bazel build //examples/cpp/hello_world:hello_world
bazel run //examples/cpp/hello_world:hello_world

Or use the provided script:

./run_bazel.sh

Standalone project (using Fory as external dependency)

For your own project using Fory as a dependency:

  1. Copy MODULE.bazel.example to MODULE.bazel in your project root
  2. Copy BUILD.standalone to BUILD in your source directory
  3. Adjust the git commit or use local_path_override for local development
# In your MODULE.bazel
bazel_dep(name = "fory", version = "0.14.1")
git_override(
    module_name = "fory",
    remote = "https://github.com/apache/fory.git",
    commit = "main",  # Use specific commit for reproducibility
)

# In your BUILD file
cc_binary(
    name = "my_app",
    srcs = ["main.cc"],
    deps = ["@fory//cpp/fory/serialization:fory_serialization"],
)

Example Overview

This example demonstrates:

  • Primitive types: Serializing integers, floats, and basic types
  • Strings: High-performance string serialization
  • Collections: Vectors and maps
  • Custom structs: Using FORY_STRUCT macro to register struct fields
  • Nested structs: Structs containing other structs
  • Enums: Enum serialization

Key Concepts

Registering Structs

Use the FORY_STRUCT macro to register struct fields for serialization:

struct Point {
  int32_t x;
  int32_t y;
};

FORY_STRUCT(Point, x, y);

Creating a Fory Instance

auto fory = fory::serialization::Fory::builder()
                .xlang(true)      // Enable cross-language serialization
                .track_ref(false) // Disable reference tracking
                .build();

// Register struct types with unique IDs
fory.register_struct<Point>(1);

Serialization and Deserialization

// Serialize
Point point{10, 20};
auto bytes_result = fory.serialize(point);
if (bytes_result.ok()) {
    auto bytes = bytes_result.value();
    // Use bytes...
}

// Deserialize
auto result = fory.deserialize<Point>(bytes.data(), bytes.size());
if (result.ok()) {
    Point deserialized = result.value();
}

Using Fory in Your Project

CMake Integration

# Link to serialization library
target_link_libraries(your_app PRIVATE fory::serialization)

Including Headers

#include "fory/serialization/fory.h"