edit-icon download-icon

C/C++ SDK preparation

Last Updated: Oct 16, 2018

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

Note:

  • The topic, producer ID, and consumer ID involved in the code need to be created on the MQ console. Message tag can be completely customized by the application. For detailed creation process, see Step 2: Create resources in Quick start guide.

  • Applications using MQ services need to be deployed on Alibaba Cloud ECS.

Download SDK

CPP supports cross-platform SDK for both Windows and Linux, and the interfaces are the same. Download the SDK package:

Decompress the package, and the following directories will be created:

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

The roles of the directories and files are as follows:

  • demo: Include a ready-to-use Windows C++ demo.

  • example:Include examples for sending normal messages, oneway messages, ordered messages, normal message consumption, and ordered message consumption. Makefile for the compilation and management of the examples is also included for Linux.

  • include: The header files to be included in users’ code.

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

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

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

    1. 64/
    2. vc_redist.x64
  • SDK_GUIDE.pdf: Document and FAQ for SDK environment preparation.

  • changelog: A list of solved problems and new features in the new version.

Linux C++ SDK

Since Dec 2, 2016, Linux CPP version added dependency on high performance boost libraries (v1.62.0), which reduced CPU resource usage and enhanced efficiency. Dependencies mainly include these 4 libraries: boost_system, boost_thread, boost_chrono, and boost_filesystem. We provide both static library and dynamic library:

Static Solution

MQ library file is in the directory of lib/lib-boost-static, and the boost library is statically linked to libonsclient4cpp.a. Service providers who have no dependent boost libraries can directly select static library solution. In the static library solution, corresponding boost library has been linked to ibonsclient4cpp.a, and you just need to link to libonsclient4cpp.a when compiling the file. The usage is as follows:

  1. cd aliyun-mq-linux-cpp-sdk //The directory for decompressed files
  2. cd example //Go to the demo directory and modify the demo file, and enter the topic name, key and related information
  3. g++ -static -I ../include -L ../lib/lib-boost-static ProducerExampleForEx.cpp -lonsclient4cpp -lpthread ldl

Note: For completely static link, make sure related static libraries such as libstdc++ and pthread are installed on your machine. libstdc++ which is installed by default does not include static libraries, so related static libraries needs to be installed through yum or > apt-get. In addition, some warning information may occur when using the method above:

  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 using completely static link, but to use statically linked lonsclient4cpp only. Link to other libraries dynamically. The usage is as follows:

  1. g++ -I ../include -L ../lib/lib-boost-static ProducerExampleForEx.cpp Wl,-dn -lonsclient4cpp Wl,-dy -lpthread ldl

In addition, since GCC 5.x introduces Dual ABI, please add the option of -D_GLIBCXX_USE_CXX11_ABI=0 when compiling links.

Dynamic Solution

MQ library file is in the directory of lib/lib-boost-share, and boost dynamic libraries and libonsclient4cpp.so need to be linked when the service providers create executable files. Since the service provides have depended on boost libraries, in scenarios where dynamic library solution needs to be selected, the following steps have to be made for the dependency of boost libraries:

  1. Download boost v1.62.0:

    boost 1.62.0

  2. Decompress boost v1.62.0:

    tar —bzip2 -xf /path/to/boost_1_62_0.tar.bz2
  3. Install boost v1.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

  4. Execute ldconfig -v|grep libboost. If there is related output, it indicates that boost dynamic libraries are in the search paths of the dynamic libraries.

  5. When executable files are created, boost dynamic libraries and MQ dynamic libraries need to be linked. The method is as follows:

    1. cd aliyun-mq-linux-cpp-sdk //The directory for decompressed files
    2. cd example //Go to the demo directory, modify the demo file, and enter the topic , key and related information that you created on the MQ console
    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 application

Windows C++ SDK

Use C++ SDK in 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, select Properties > Configuration Manager, and set Active Solution Configuration to Release, and Active Solution Platform to x64.

    Platform Configuration 1

    Platform Configuration 2

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

    Set Output Directory

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

    Configure Header File 1

    Configure Header File 2

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

    Additional Library Setting

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

    ONSCLient4CPP.lib Setting

  7. Right click the project, and select Properties > C/C++ - General > Preprocessor Definitions: add Macro WIN32.

    Set Marco WIN32

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

  1. Follow the steps above 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: If you want to skip complex settings, you can use the SDK demo that has been set up. Download and decompress the SDK, go to the demo directory, and open the project with VS2015.

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 application.

Thank you! We've received your feedback.