Set up the ApsaraMQ for RocketMQ C++ SDK V1.x.x on Linux or Windows so you can compile and run the included examples to send and receive messages.
We recommend that you use the latest RocketMQ 5.x SDKs. These SDKs are fully compatible with ApsaraMQ for RocketMQ 5.x brokers and provides more functions and enhanced features. For more information, see Version Guide.
Alibaba Cloud only maintains RocketMQ 4.x, 3.x, and TCP client SDKs. We recommend that you use them only for existing business.
The latest RocketMQ 5.x SDKs are fully compatible with ApsaraMQ for RocketMQ 5.x brokers and offer more features and better performance. For details, see Release notes. RocketMQ 4.x, 3.x, and TCP client SDKs are in maintenance mode. Use them only for existing workloads.
Prerequisites
Before you begin, make sure that you have:
An Alibaba Cloud Elastic Compute Service (ECS) instance to deploy your application
Topics and groups created in the ApsaraMQ for RocketMQ console -- specify custom message tags in your application code as needed
Download the SDK
The C++ SDK V1.x.x supports both Windows and Linux with a unified API. On Linux, CentOS 6/RHEL 6 and CentOS 7/RHEL 7 are supported. For download links, see Release notes.
After you download and extract the package, the SDK has the following directory structure:
aliyun-mq-linux-cpp-sdk/
├── demo/ # Windows only: preconfigured Visual Studio demo project
├── example/ # Send/receive examples for normal, ordered, and one-way messages
│ # On Linux, includes a Makefile for compiling examples
├── include/ # Header files required by your application
├── lib/
│ ├── lib-boost-share/
│ │ └── libonsclient4cpp.so # 64-bit dynamic library (Linux)
│ ├── lib-boost-static/
│ │ └── libonsclient4cpp.a # 64-bit static library (Linux)
│ └── 64/ # 64-bit DLL (Windows)
│ └── vc_redist.x64 # Visual C++ 2015 runtime (required if VS2015 is not installed)
├── SDK_GUIDE.pdf # SDK documentation and FAQ
└── changelog # Bug fixes and new features per releaseSet up the Linux environment
Starting from December 2, 2016, the Linux C++ SDK depends on Boost 1.62.0 (boost_system, boost_thread, boost_chrono, and boost_filesystem). These libraries reduce CPU resource usage and improve operation efficiency.
Two linking strategies are available: static and dynamic. Choose the one that fits your project.
Static library
In the static library approach, the Boost libraries are linked into libonsclient4cpp.a. Link only to this single archive when you compile.
Build with Make:
Navigate to the extracted SDK directory:
cd aliyun-mq-linux-cpp-sdkOpen the example source files in
example/and enter your topic name, group ID, and access key credentials.Build the examples with static linking:
cd example make static=1
Fully static linking requires the static versions of libstdc++ and pthread on your machine. The default libstdc++ installation typically does not include static libraries. Install them with yum or apt-get as needed.
A fully static build may produce this warning:
warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linkingRecommended approach -- partial static linking:
To avoid glibc compatibility issues, statically link only onsclient4cpp and dynamically link everything else:
g++ -ggdb -Wall -O3 -I../include \
../example/ProducerExampleForEx.cpp \
-Wl,-static -lonsclient4cpp -L../lib/lib-boost-static/ \
-Wl,-Bdynamic -lpthread -ldl -lrt \
-o ../example/ProducerExampleForExGCC 5.x introduced Dual ABI. When you compile with GCC 5.x or later, add -D_GLIBCXX_USE_CXX11_ABI=0 to the compile command.
Dynamic library
In the dynamic library approach, the SDK ships libonsclient4cpp.so in lib/lib-boost-share/. Install the Boost 1.62.0 shared libraries separately and link them at compile time.
Download Boost 1.62.0.
Extract the package:
tar --bzip2 -xf /path/to/boost_1_62_0.tar.bz2Build and install Boost:
cd path/to/boost_1_62_0 ./bootstrap.sh ./b2 link=shared runtime-link=shared ./b2 installVerify that the Boost shared libraries are in the dynamic linker search path: If the command produces output, the libraries are correctly installed.
ldconfig -v | grep libboostCompile and run your application:
cd aliyun-mq-linux-cpp-sdk/example g++ -Wall -Wno-deprecated \ -L ../lib/lib-boost-share/ -I ../include/ \ ProducerExampleForEx.cpp \ -lonsclient4cpp -lboost_system -lboost_thread \ -lboost_chrono -lboost_filesystem -lpthread export LD_LIBRARY_PATH="../lib/lib-boost-share/" ./a.out
Set up the Windows environment
Visual Studio 2015
Create a new project in Visual Studio 2015.


Right-click the project and choose Properties > Configuration Manager. Set Active solution configuration to release and Active solution platform to x64.


Open Properties > Configuration Properties > General. Set Output Directory to a path (referred to as /A below). Copy all files from the SDK
lib/64/directory into /A.
Open Properties > Configuration Properties > C/C++ > General. Set Additional Include Directories to a path (referred to as /B below). Copy the header files from the SDK
include/directory into /B.

Open Properties > Configuration Properties > Linker > General. Set Additional Library Directories to /A.

Open Properties > Configuration Properties > Linker > Input. Add ONSClient4CPP.lib to Additional Dependencies.

Open Properties > Configuration Properties > C/C++ > Preprocessor. Add WIN32 to Preprocessor Definitions.

After you complete these steps, click Build to compile your project. Copy the DLL file to the directory that contains your executable (or to a system directory) before you run the program.
Non-Visual Studio 2015 environments
Follow the same configuration steps described in the "Visual Studio 2015" section above.
Install vc_redist.x64 (the Visual C++ 2015 runtime) on the target machine.
To skip manual configuration, use the preconfigured demo project. Extract the SDK package, open the project in the demo/ directory with Visual Studio 2015, and update the file paths to match your local setup.


