edit-icon download-icon

Use C++ SDK

Last Updated: Mar 20, 2018

Prerequisites

  • Install a compiler supporting C++ 11 or later, including Visual Studio 2015 and later or GCC 4.9 and later.

  • Install CMake 3.0 or later.

  • The computer has a memory of 4 GB or larger.

Install C++ SDK

  1. Run the following command to clone the source codes of C++ SDK.

    1. git clone https://github.com/aliyun/aliyun-openapi-cpp-sdk.git
  2. Install the SDK.

    • Install C++ SDK on the Windows platform

      Go to the sdk_build directory of the source code, and use Visual Studio to open the alibabacloud-sdk.sln file to generate the solution.

      Or run the following command at the command prompt of Visual Studio to install the C++ SDK:

      1. msbuild ALL_BUILD.vcxproj
      2. msbuild INSTALL.vcxproj
    • Install C++ SDK on the Linux platform

    1. Install dependency libraries, including libcurl, libopenssl, libuuid, and libjsoncpp.

      • Run the following command in the Debian/Ubuntu system to install dependency libraries.
        1. sudo apt-get install libcurl4-openssl-dev libssl-dev uuid-dev libjsoncpp-dev
      • Run the following command in the Redhat/Fedora system to install dependency libraries.
        1. sudo dnf install libcurl-devel openssl-devel libuuid-devel libjsoncpp-devel
    2. Run the following command to compile and install the C++ SDK.

      1. make
      2. sudo make install

Initialize C++ SDK

Before using the Alibaba Cloud C++ SDK service client to call a service, you must use AlibabaCloud::InitializeSdk to initialize the SDK first and use AlibabaCloud::ShutdownSdk to release resources at last.

  1. #include <alibabacloud/core/AlibabaCloud.h>
  2. int main(int argc, char** argv)
  3. {
  4. AlibabaCloud::InitializeSdk();
  5. // ......
  6. AlibabaCloud::ShutdownSdk();
  7. return 0;
  8. }

Set up credentials

When using the Alibaba Cloud C++ SDK to access Alibaba Cloud services, you need an Alibaba Cloud account for authentication.

C++ SDK supports the following authentication methods.

Authentications Descriptions
AccessKey Use the AccessKeyID/Secret to do the authentication
StsToken Use the STS Token to do the authentication
RamRoleArn Use the AssumeRole of the RAM account to do the authentication
EcsRamRole Use the RAM role of an ECS instance to do the authentication
RsaKeyPair Use the RSA key pair to do the authentication (supported only on Japan site)

This document uses AccessKey as an example to illustrate how to set up credentials.

Note: To ensure the security of your account, we recommend using your RAM account instead of the primary account. The primary account has full access to all of your cloud services, while the RAM account has limited access granted by the primary account to the cloud services.

Firstly, create an AccessKey as described in Create an AccessKey, and then set up your credentials when initializing the client as follows:

Note: Do not disclose any code containing your AccessKey (do not commit the code to public GitHub projects). Otherwise, your Alibaba Cloud account may be compromised.

  1. // Create a client instance
  2. ClientConfiguration configuration("<your-region-id>");
  3. EcsClient client("<your-access-key-id>", "<your-access-key-secret>", configuration);

Configure the service client

The Alibaba Cloud C++ SDK provides corresponding Alibaba Cloud APIs for the client to use. The client service complies with the following naming convention:

AlibabaCloud::Service::ServiceClient

For example, use AlibabaCloud::Ecs::EcsClient to build an ECS client.

You can use ClientConfiguration to control some running functions of the service client, as the following code example shows.

  1. // Configure an ecs instance
  2. ClientConfiguration configuration;
  3. configuration.setRegionId("cn-hangzhou");
  4. configuration.setEndpoint("ecs-cn-hangzhou.aliyuncs.com");
  5. EcsClient client("<your-access-key-id>", "<your-access-key-secret>", configuration);

where:

  • regionId is the ID of the region where the service is located.

  • endpoint is the service endpoint.

  • proxy is the network proxy.

Use CMake to build a program

CMake is an automatic cross-platform and open-source compiling system. It uses a file named CMakeLists.txt to describe the dependency relation between the compiling process and the application, and finally generates the project file that suits your platform most.

Follow these steps to use CMake to build a program:

  1. Create a CMake project:

    1. Run the following command to create a project directory.

      1. mkdir my_example
    2. Create a CMakeLists.txt file under the created project directory.

      This file is used for describing your project name, executable files, source files and link libraries. For more information, see Official site of CMake.

      1. # Set the lowest Cmake version required by the project
      2. cmake_minimum_required(VERSION 3.0)
      3. # Set the project name
      4. project(my-example)
      5. # Set C++ 11 as the default version
      6. set(CMAKE_CXX_STANDARD 11)
      7. # Set the SDK library installation catalogue
      8. link_directories(/usr/local/lib64)
      9. # Set the application name and the original file
      10. add_executable(my-example
      11. main.cc)
      12. # Dynamic link SDK library file
      13. add_definitions(-DALIBABACLOUD_SHARED)
      14. # Specify the SDK library file to link
      15. target_link_libraries(my-example
      16. alibabacloud-sdk-core
      17. alibabacloud-sdk-ecs)
  2. Use CMake to compile a program:

    1. Run the following command to create a compiling directory under the project folder.

      1. mkdir build
    2. Go to the created compiling directory and run the camke command.

      1. cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/usr/local ..

      The following are compiling options of the Alibaba Cloud C++ SDK:

      • BUILD_SHARED_LIBS: The file output type of the Alibaba Cloud SDK library. valid value:

        ON: Dynamic library. This is the default value.

        OFF: Static library.

        Note: Before dynamically linking an Alibaba Cloud SDK, you must define the preprocessor symbol ALIBABACLOUD_SHARED.

      • BUILD_TESTS: Whether to compile the unit test module. Valid value:

        ON: Compile the unit test module. This is the default value.

        OFF: Do not compile the unit test module

      • TARGET_OUTPUT_NAME_PREFIX: The prefix of the output file name. The default value is alibabacloud-sdk-.

    3. After Makefile is generated, run the make command to compile the application.

Thank you! We've received your feedback.