The Apache RocketMQ 5.0 SDK for C++ is built on gRPC and must be compiled from source. This guide covers installing dependencies, building gRPC, and compiling the SDK so you can start sending and receiving messages with ApsaraMQ for RocketMQ.
Prerequisites
Install the following tools and libraries before you begin. Each component links to its official installation guide.
| Component | Required version | Purpose |
|---|---|---|
| GCC | 4.8.5+ (10.2+ recommended) | C++11 required. For C++17, use GCC 7+ with -std=c++17 or -std=gnu++17. |
| CMake | 3.13+ | Build system for the SDK and gRPC |
| OpenSSL | 1.1.1 | TLS/SSL library required by gRPC |
| protobuf | Any compatible version | Protocol Buffers serialization library |
The SDK shares the same compiler compatibility matrix as gRPC.
Step 1: Install gflags
Download, compile, and install gflags 2.2.2:
wget -O gflags-2.2.2.tar.gz https://github.com/gflags/gflags/archive/v2.2.2.tar.gz
tar -xvzf gflags-2.2.2.tar.gz
cd gflags-2.2.2/
mkdir build && cd build
cmake -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON -DINSTALL_HEADERS=ON -DINSTALL_SHARED_LIBS=ON -DINSTALL_STATIC_LIBS=ON ..
make
make install
cdAdd the library path so the linker can find gflags at runtime:
cat >> ~/.bashrc << EOF
export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/usr/local/lib
EOF
source ~/.bashrcStep 2: Build and install gRPC
Use gRPC 1.46.3. Later versions may not be compatible with the Apache RocketMQ 5.0 SDK for C++.
Set the installation directory and update PATH:
export MY_INSTALL_DIR=$HOME/grpc
mkdir -p $MY_INSTALL_DIR
export PATH="$MY_INSTALL_DIR/bin:$PATH"Clone and build gRPC:
git clone --recurse-submodules \
-b v1.46.3 --depth 1 --shallow-submodules \
https://github.com/grpc/grpc
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \
../..
# Adjust -j based on available CPU cores.
make -j 32
make install
popdTip: The-j 32flag runs 32 parallel compilation jobs. Reduce this number on machines with fewer CPU cores or limited memory. For example, use-j 4on a 4-core machine.
Step 3: Compile the SDK from source
The SDK supports two build systems: CMake (recommended) and Bazel.
Option A: Build with CMake (recommended)
wget -O rocketmq-clients-cpp-5.0.3.tar.gz https://github.com/apache/rocketmq-clients/archive/refs/tags/cpp-5.0.3.tar.gz
tar -xvzf rocketmq-clients-cpp-5.0.3.tar.gz
cd rocketmq-clients-cpp-5.0.3/cpp
mkdir build && cd build
cmake ..
makeOption B: Build with Bazel
wget -O rocketmq-clients-cpp-5.0.3.tar.gz https://github.com/apache/rocketmq-clients/archive/refs/tags/cpp-5.0.3.tar.gz
tar -xvzf rocketmq-clients-cpp-5.0.3.tar.gz
cd rocketmq-clients-cpp-5.0.3/cpp
bazel build //...Step 4: Create the required resources
Before you run any sample code, create the following resources in the ApsaraMQ for RocketMQ console:
Instance: a RocketMQ service instance
Topic: the destination for messages
Consumer group: a logical grouping of consumers
For detailed instructions, see Create resources.
Step 5: Run the sample code
After you create the resources, update the parameters in the sample code based on the resource information you obtained in Step 4, then compile and run the examples.