All Products
Search
Document Center

ApsaraMQ for RocketMQ:Environment preparation (V1.x.x)

Last Updated:Sep 23, 2024

Before you use the SDK for C++ to connect to ApsaraMQ for RocketMQ to send and receive messages, you must prepare the environment. This topic describes the preparations, instructions, and precautions for using the SDK for C++.

Important
  • 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 Release notes.

  • Alibaba Cloud only maintains RocketMQ 4.x, 3.x, and TCP client SDKs. We recommend that you use them only for existing business.

Before you start, take note of the following items:

  • This topic describes only the environment preparation for the SDK for C++ V1.x.x.

  • You must go to the ApsaraMQ for RocketMQ console to create the topics and groups that are involved in the code. You can specify custom message tags in your application. For information about how to create a message tag, see Create resources.

  • Applications that use ApsaraMQ for RocketMQ must be deployed on Alibaba Cloud Elastic Compute Service (ECS) instances.

Download the SDK for C++

The SDK for C++ can be used on Windows and Linux by using the same API. CentOS 6/RHEL 6 and CentOS 7/RHEL 7 are supported on Linux. For information about how to download the SDK for C++, see Release notes.

After you download and decompress the package, the following directory structure is displayed:

  • demo/ (only for Windows)

    Contains a created Windows C++ demo.

  • example/

    Contains examples on how to send and receive normal messages and ordered messages and examples on how to send messages in one-way mode. On Linux, this directory also contains the Makefile file that you can use to compile and manage the examples.

  • include/

    Contains the header files that are required by your programs.

  • lib/

    • The following code shows the SDK subdirectories on Linux, including a 64-bit static library and a 64-bit dynamic library:

      lib-boost-share/
        libonsclient4cpp.so
      lib-boost-static/
        libonsclient4cpp.a                        
    • The following code shows the SDK subdirectory on Windows. It is a 64-bit SDK DLL library. If Visual Studio 2015 is not installed, you must install vc_redist.x64, which is the runtime 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 new releases.

Linux C++ SDK

Starting from December 2, 2016, Linux CPP has added high-performance Boost libraries (V1.62.0) as its dependencies. This reduces CPU resource usage and improves operation efficiency. Linux CPP depends on boost_system, boost_thread, boost_chrono, and boost_filesystem libraries. The static library solution and the dynamic library solution are available for Linux CPP.

Static library solution

The library files of ApsaraMQ for RocketMQ are in the lib/lib-boost-static directory, and the Boost libraries are statically linked to libonsclient4cpp.a. Service providers who do not depend on Boost libraries can use the static library solution. In the static library solution, the Boost libraries are linked to libonsclient4cpp.a. When you compile a file, you need to link only to libonsclient4cpp.a. The following code shows how to use this method:

cd aliyun-mq-linux-cpp-sdk  // The path to which the downloaded SDK for C++ package is decompressed.
cd example                  // Go to the example directory and enter the information about the topic that you created in the ApsaraMQ for RocketMQ console and the information about the key in the example file.
make static=1            
Important

For completely static links, make sure that static libraries that are related to the C++ standard library (libstdc++) and POSIX threads (pthread) are installed on your machine. libstdc++ that is installed by default does not contain static libraries. You must run the yum or apt-get command to install the static libraries.

When you use the preceding method, the following warning message may be returned:

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

We recommend that you statically link only to onsclient4cpp and dynamically link to other libraries instead of using completely static links. The following code shows how to use this method:

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        

Dual ABI is introduced to GCC 5.x. When you compile a link, you must add the -D_GLIBCXX_USE_CXX11_ABI=0 option.

Dynamic library solution

The library files of ApsaraMQ for RocketMQ are in the lib/lib-boost-share directory. When service providers generate executable files, the service providers must link the Boost dynamic libraries to libonsclient4cpp.so. If service providers depend on Boost libraries and want to use the dynamic library solution, perform the following steps for the dependency of Boost libraries:

  1. Download Boost 1.62.0.

    boost 1.62.0

  2. Decompress the Boost 1.62.0 package.

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

  3. Install Boost 1.62.0.

    1. Run the cd path/to/boost_1_62_0 command to go to the directory to which the Boost 1.62.0 package is decompressed.

    2. Run the ./bootstrap.sh command to configure Boost.

    3. Run the ./b2 link=shared runtime-link=shared command to compile Boost.

    4. Run the ./b2 install command to install Boost.

  4. Run the ldconfig -v|grep libboost command. If outputs are displayed, the Boost dynamic libraries are in the search paths of dynamic libraries.

  5. Link the Boost dynamic libraries to the ApsaraMQ for RocketMQ dynamic libraries when executable files are generated. The following code shows the details:

    cd aliyun-mq-linux-cpp-sdk  // The path to which the downloaded SDK for C++ package is decompressed.
    cd example                  // Go to the example directory and enter the information about the topic that you created in the ApsaraMQ for RocketMQ console and the information about the key in the example file.
    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

Use the SDK for C++ in the Visual Studio 2015 environment

  1. Use Visual Studio 2015 to create your project.createprojectnextfinish

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

  3. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > General, click Output Directory, and then select /A. Copy all files in the 64-bit lib directory to the /A directory based on the setting of the Active solution platform parameter.path

  4. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > C/C++ > General, click Additional Include Directories, and then select /B. Copy the header file in the include directory to the /B directory.otherdirectoryadditional

  5. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > Linker > General, click Additional Library Directories, and then select /A.additionalA

  6. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > Linker > Input, click Additional Dependencies, and then select ONSClient4CPP.lib.ONSClient4CPP.lib

  7. Right-click the project and select Properties. In the window that appears, choose Configuration Properties > C/C++ > Preprocessor, and then click Preprocessor Definitions to add the WIN32 macro.WIN32

Use the SDK for C++ in an environment other than Visual Studio 2015

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

  2. Install vc_redist.x64.

    Important

    To prevent complex settings, you can use the preconfigured SDK for C++ demo. You can download and decompress the SDK for C++ package, go to the demo directory, and then open the project in Visual Studio 2015.

    The following figures provide an example.

    demopropertiessetfilepath

    After you perform the preceding operations, the environment is compiled. Then, you can click Build to compile an executable program and copy the DLL file to the directory in which the executable program resides or to the system directory to run the program.

References

Sample code