This topic describes the preparations, instructions, and precautions for accessing Message Queue for Apache RocketMQ by using the SDK for C++ V1.X.X so that you can use the SDK for C++ to send and receive messages.

Before you use the SDK for C++, take note of the following points:

  • This topic describes only the SDK for C++ V1.X.X. If you want to use the SDK for C++ V2.0.0, see Prepare the environment (V2.0.0) or download the SDK for C++ V2.0.0. For more information about how to use the SDK for C++ V2.0.0, see Prepare the environment (V2.0.0).
  • Go to the console to create the topics and group IDs that are referred to in the code. The message tag can be specified by application users. For more information about how to create a message tag, see Create resources.
  • An application that uses Message Queue for Apache RocketMQ must be deployed on Alibaba Cloud Elastic Compute Service (ECS).

Download Tablestore SDK for C++

The C++ SDK supports Windows and Linux operating systems with identical interfaces. The CentOS 6(RHEL 6) and CentOS 7(RHEL 7) series are supported in Linux. For more information about how to download the C++ SDK, see Release notes.

After the download is complete, decompress the package. The directory structure is as follows:

  • demo/(for Windows only)

    Contains a created Windows C++ demo demo.

  • example/

    Examples include normal message sending, one-way message sending, ordered message sending, normal message consumption, and ordered message consumption. Linux also includes Makefile for example compilation and management.

  • include/

    This directory contains header files required by your own programs.

  • lib/
    • The following subdirectories of the Linux SDK are the static and dynamic libraries of 64.
      lib-boost-share/
        libonsclient4cpp.so
      lib-boost-static/
        libonsclient4cpp.a                        
    • The Windows SDK subdirectory is as follows, which is the dll library of the SDK in a 64-bit system. If no Visual Studio 2015 environment is installed, copy the installation vc_redist.x64. This is the run time environment for Visual C++ 2015.
      64/
      vc_redist.x64                        
  • SDK_GUIDE.pdf

    Contains the documentation and frequently asked questions (FAQ) about how to prepare the SDK environment.

  • changelog

    Contains bug fixes and new features in the new releases.

Linux C++ SDK

Since December 02, 2016, the Linux CPP version relies on the high-performance boost library (version 1.62.0), which not only reduces the CPU resource utilization rate, but also improves the operation efficiency. Currently, it mainly relies on four libraries: boost_system, boost_thread, boost_chrono, and boost_filesystem. There are two solutions: static library and dynamic library.

Static Solution

The Message Queue for Apache RocketMQ library file is in the lib/lib-boost-static directory. The boost library is statically linked to the libonsclient4cpp.a. For business parties that do not rely on the boost library, you can directly select the static library solution. In the static library solution, the corresponding boost library has been linked to the libonsclient4cpp.a. You only need to link the libonsclient4cpp.a during compilation, and no other operations are required. The following code provides an example on how to set the fields:

cd aliyun-mq-linux-cpp-sdk // The path where the downloaded SDK is decompressed.
cd example // Go to the example directory, modify the example file, and enter the information about the topic and key that you created.
make static=1            
Important For full static links, make sure that libstdc++, pthread and other related static libraries are installed on the machine. By default, libstdc++ does not have static libraries installed, so you need to install related static libraries through yum or > apt-get.

In addition, some warning messages will appear as follows when using the above method.

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

It is recommended that the best way is not to use complete static links, but only to statically link onsclient4cpp, and other libraries can be dynamically linked. The following code provides an example on how to set the fields:

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        

In addition, because GCC 5.x introduces Dual ABI, when compiling links, add -D_GLIBCXX_USE_CXX11_ABI=0 compiler options.

Dynamic solutions

The Message Queue for Apache RocketMQ library file is in the lib/lib-boost-share directory, and the business side needs to link the boost dynamic library and libonsclient4cpp.so when generating the executable file. if the business side has already relied on the boost library and needs to select a dynamic library solution, the following work needs to be done to rely on the boost library:

  1. Download Boost 1.62.0.

    boost 1.62.0

  2. Decompress boost 1.62.0.

    tar --bzip2 -xf /path/to/boost_1_62_0.tar.bz2

  3. Install boost version 1.62.0:
    1. Go to the path after boost 1.62.0 decompression: 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
  4. The execution ldconfig -v|grep libboost. If there is relevant output, it indicates that the boost dynamic library is in the dynamic library search path.
  5. When generating an executable file, you need to link the boost dynamic library and the Message Queue for Apache RocketMQ dynamic library. The method is as follows.
    cd aliyun-mq-linux-cpp-sdk // The path where the downloaded SDK is decompressed.
    cd example // Go to the example directory, modify the example file, and enter the topics and keys that you created in the Message Queue for Apache RocketMQ console.
    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/"  //Add dynamically loaded search paths.
    ./a.out  //Run the program.

Windows C++ SDK

Visual Studio 2015 environment using C++ SDK

  1. Use Visual Studio 2015 to create your project. createprojectnextfinish
  2. Right-click the project and choose Properties > Configuration Manager. Set Active Solution Configuration to release and Active Solution Platform to x64. x64CM
  3. Right-click the project and choose Properties. In the window that appears, choose Configuration Properties > General, click Output Directory, and select /A. Copy all the files in the 64-bit lib directory to the output directory /A, as set up for the active solution platform. path
  4. Right-click the project and choose Properties. In the window that appears, choose Configuration Properties > C/C++ > General, click Additional Include Directories, and select /B. Copy the header files in the include directory to the containing directory: /B. otherdirectoryadditional
  5. Right-click the project and choose Properties. In the window that appears, choose Configuration Properties > Linker > General, click Additional Library Directories, and select /A. additionalA
  6. Right-click the project and choose Properties. In the window that appears, choose Configuration Properties > Linker > Input, click Additional Dependencies, and select ONSClient4CPP.lib. ONSClient4CPP.lib
  7. Right-click the project and choose Properties > Configuration Properties > C/C++-General > Preprocessor Definition: Add WIN32 Macro. WIN32

Visual Studio non -2015 environment using the C++ SDK

  1. First, you need to configure it based on the Visual Studio 2015 environment. The configuration process is the same as using the C++ SDK in the Visual Studio 2015 environment.
  2. Install vc_redist.x64. This is the Visual C++ 2015 run time environment.
    Important If you do not want to make complicated settings, you can also use the specified SDK demo, download the SDK, decompress the SDK, and then go to the demo directory to open the project by using the Visual Studio 2015.

    Sample code:

    demopropertiessetfilepath

    At this point, the compiling environment is ready. Click Build to compile the executable program, and then copy the dll to the directory of the executable program to run, or copy the dll to the system directory.

Upgrade SDK for v1.x.x to v2.0.0

Currently, the latest version of SDK v2.0.0 and SDK v1.x.x maintain API forward compatibility (that is, the new version is compatible with the old version). ABI is incompatible due to different implementation methods.

Perform the following steps:

  1. Replace the header file of the new version of the SDK with the header file of the old version.
  2. Copy all the dynamic libraries in the new version of the lib directory to the directory where the original version of the dynamic library is stored.
  3. If the static link scheme was used before, remove the -static parameter and change it to the default dynamic link scheme.
  4. Modify the compilation script, add -lrocketmq_client_core parameters, and link the new kernel dynamic library.
  5. Recompile the project.
  6. If the upgrade fails, you can roll back the project to the pre-upgrade status and recompile it.

References