All Products
Search
Document Center

Prepare the C_C++ SDK environment

Last Updated: Apr 16, 2019

The following preparation is required for accessing RocketMQ through the C++ SDK.

Note:

  • You need to create the topic and group ID involved in the code in the RocketMQ console first. The message tag can be specified by the application users. For more information about the creation process, see Step 2: Create resources in Quick start for primary accounts.

  • Applications that use RocketMQ must be deployed on Alibaba Cloud ECS instances.

Download SDK

C++ supports cross-platform SDKs for both Windows and Linux, and the APIs are the same. For more information about the download URL of the latest C++ SDK, see Release Notes.

Download and decompress the .zip package of the C++ SDK. The .zip package contains the following directories and files:

  • example/
  • include/
  • lib/
  • SDK_GUIDE.pdf
  • changelog

The preceding directories and files serve the following purposes:

  • demo: This folder contains a created Windows C++ demo.

  • example:This folder contains examples for sending and consuming normal messages and ordered messages and examples for sending messages in one-way mode. Besides, the package for Linux also contains a file Makefile for compiling and managing the examples.

  • include: This folder contains the header file to be included in your program.

  • lib: The Linux SDK sub-directories are as follows, which are 64-bit status library and dynamic library, respectively.

  1. lib-boost-share/
  2. libonsclient4cpp.so
  3. lib-boost-static/
  4. libonsclient4cpp.a

The Windows SDK sub-directories are as follows, which is the 64-bit SDK DLL library. If Visual Studio 2015 is not installed, install vc_redist.x64. This is the runtime environment for Visual C++ 2015.

  1. 64/
  2. vc_redist.x64
  • SDK_GUIDE.pdf: This file describes how to prepare the SDK environment and contains FAQ.

  • changelog: This file lists the problems that have been fixed and the new features of the new version.

Linux C++ SDK

From December 2, 2016, Linux C++ SDK added dependency on high performance Boost libraries (v1.62.0), which reduces the CPU resource usage and enhances the efficiency. Currently, Linux C++ SDK depends on four libraries: boost_system, boost_thread, boost_chrono, and boost_filesystem. We provide solutions for both static library and dynamic library.

Static solution

The RocketMQ library file is in the directory lib/lib-boost-static and the Boost libraries are statically linked to libonsclient4cpp.a. Service providers who have no dependent Boost libraries can directly choose the static library solution. In the static library solution, the Boost libraries have been linked to ibonsclient4cpp.a, and you just need to link to libonsclient4cpp.a when compiling the file. The code is as follows:

  1. cd aliyun-mq-linux-cpp-sdk //The path to which the downloaded SDK package is decompressed.
  2. cd example //Go to the demo directory and enter the topic and key you created and other information in the demo file.
  3. make static=1

Note: For completely static links, make sure that required static libraries such as libstdc++ and pthread have been installed on your machine. libstdc ++ that is installed by default does not contain static libraries. You need to run the yum or > apt-get command to install the static libraries. The following warning message may be returned when you use the preceding method:

  1. warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

The best practice is to avoid the use of completely static links, but to use statically linked lonsclient4cpp only and link to other libraries dynamically. The code is as follows:

  1. g++ -ggdb -Wall -O3 -I../include ../example/ProducerExampleForEx.cpp -Wl,-static -lonsclient4cpp -L../lib/lib-boost-static/ -Wl,-Bdynamic -lpthread -ldl -lrt -o ../example/ProducerExampleForEx

Additionally, since GCC 5.x introduces Dual ABI, you need to add -D_GLIBCXX_USE_CXX11_ABI=0 when compiling the links.

Dynamic solution

The RocketMQ library file is in the directory lib/lib-boost-share. When generating executable files, service providers need to link the Boost dynamic libraries to libonsclient4cpp.so. Since the service providers have depended on Boost libraries, in scenarios where the dynamic library solution is required, perform the following steps for the dependency of Boost libraries:

  1. Download Boost 1.62.0:

boost 1.62.0

  1. Decompress the Boost 1.62.0 package:
tar —bzip2 -xf /path/to/boost_1_62_0.tar.bz2
  1. Install Boost 1.62.0:

1) cd path/to/boost_1_62_0

2) Configure Boost: ./bootstrap.sh

3) Compile Boost: ./b2 link=shared runtime-link=shared

4) Install Boost: ./b2 install

  1. Run ldconfig -v|grep libboost. If the system makes output, the boost dynamic libraries are in the search paths of the dynamic libraries.

  2. When executable files are generated, you need to link Boost dynamic libraries to the RocketMQ dynamic library. The method for this is as follows:

  1. cd aliyun-mq-linux-cpp-sdk //The path to which the downloaded SDK package is decompressed.
  2. cd example //Go to the demo directory and enter the topic and key you created in the RocketMQ console and other information in the demo file.
  3. g++ -Wall -Wno-deprecated -L ../lib/lib-boost-share/ -I ../include/ ProducerExampleForEx.cpp -lonsclient4cpp -lboost_system -lboost_thread -lboost_chrono -lboost_filesystem -lpthread
  4. export LD_LIBRARY_PATH="../lib/lib-boost-share/" //Add dynamically loaded search paths.
  5. ./a.out //Run the program.

Windows C++ SDK

Use C++ SDK in the Visual Studio 2015 environment

  1. Use Visual Studio 2015 to create your project.

    Create your project 1

    Create your project 2

    Create your project 3

  2. Right-click the project, choose Properties > Configuration Manager, and set Active Solution Configuration to release and Active Solution platform to x64.

    platformconfig1

    Platform configuration 2

  3. Right-click the project, and choose Properties > Configure Properties > General > Output Directory: /A. Based on the setting of Active Solution Platform, copy all files in the 64-bit lib directory to the output directory /A.

    set the output directory

  4. Right-click the property, and choose Properties > Configure Properties > C/C++ - General > Additional Include Directories: /B. Copy the header files in the include directory to the include directory /B.

    additionalincludedir

    configure the header file2

  5. Right-click the project, and choose Properties > Configure Properties > Linker > General > Additional Library Directories: /A.

    additional library setting

  6. Right-click the project, and choose Properties > Configure Properties > Linker > Input > Additional Dependencies: ONSClient4CPP.lib.

    add this

  7. Right-click the project, and choose Properties > Configure Properties > C/C++ - General > Preprocessor Definitions: Add theWIN32 macro.

    Set the WIN32 macro

Use C++ SDK in non-Visual Studio 2015 environment

  1. Follow the preceding steps to configure your project based on the Visual Studio 2015 environment.

  2. Install vc_redist.x64. This is the runtime environment for Visual C++ 2015.

    Note: To avoid implementing complex settings, use the SDK demo that has already been set up. Download and decompress the SDK package, go to the demo directory, and run the project with Visual Studio 2015.

    demo_1

    demo 2

    demo 3

Now the compilation environment has been set up. Click Build to compile the executable application, and copy the DLL to the directory of the executable application or to the system directory to run the DLL.