Application C++ SDK
IoTDA provides an application SDK in C++ for developers. This topic describes how to install and configure the C++ SDK and how to use it to call application-side APIs.
SDK Installation on Linux
- Obtain dependency packages.
- The required third-party software packages are contained in the package management tools of most Linux OSs, for example, Debian and Ubuntu.
sudo apt-get install libcurl4-openssl-dev libboost-all-dev libssl-dev libcpprest-dev
- spdlog needs to be installed using the source code.
git clone https://github.com/gabime/spdlog.git cd spdlog mkdir build cd build cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON .. // Used to generate a dynamic library make sudo make install
- The required third-party software packages are contained in the package management tools of most Linux OSs, for example, Debian and Ubuntu.
- Compile and install the SDK.
git clone https://github.com/huaweicloud/huaweicloud-sdk-cpp-v3.git cd huaweicloud-sdk-cpp-v3 mkdir build cd build cmake .. make sudo make install
After the operations are complete, the installation directory of the C++ SDK is /usr/local.
SDK Installation on Windows
- Install vcpkg and use it to install the required software packages.
vcpkg install curl cpprestsdk boost openssl spdlog
- Use CLion for compilation.
- Use CLion to open the huaweicloud-sdk-cpp-v3 directory.
- Choose File > Settings.
- Choose Build > Execution > Deployment > CMake.
- Add the following configuration to CMake options:
-DCMAKE_TOOLCHAIN_FILE={your vcpkg install dir}/scripts/buildsystems/vcpkg.cmake - Right-click CMakeLists.txt and choose Load CMake Project from the shortcut menu.
- Set the Clion compilation toolchain to Microsoft Visual C++ (MSVC). On the CMake configuration page in 2.c, set Toolchain to Visual Studio. Do not select other compilers such as MinGW. (The MSVC compiler is required on the Windows platform. If you select other compilers such as MinGW, an error will be reported.) You can also select the build type for the compiled binary file. You can select Debug or Release from the Build Type drop-down list.
- Configure the architecture and platform of the target file. The Windows platform supports the compilation of SDK link library files of different CPU architectures (x64 and x86). You can configure the architecture as required. Choose Build > Execution > Deployment > Toolchains and select the supported CPU architecture from the Architecture drop-down list.
- Click Build to start compilation. The compilation result is displayed in the Clion console.
- Installing the C++ SDK After the compilation is complete, choose Build > Install. The C++ SDK installation directory is C:\Program File (x86)\huaweicloud-sdk-cpp-v3.
Sample Code
The following sample shows how to use the C++ SDK to call API for querying the device list.
- Create a credential.
- Create and initialize an IoTDAClient instance.
- Instantiate a request object.
- Call the API for querying the device list.
#include <cstdio> #include <iostream> #include <huaweicloud/core/exception/Exceptions.h> #include <huaweicloud/core/Client.h> #include <huaweicloud/iotda/v5/IoTDAClient.h> using namespace HuaweiCloud::Sdk; using namespace HuaweiCloud::Sdk::Core; using namespace HuaweiCloud::Sdk::Core::Utils; using namespace HuaweiCloud::Sdk::Core::Exception; using namespace HuaweiCloud::Sdk::Iotda::V5; using namespace HuaweiCloud::Sdk::Iotda::V5::Model; using namespace std; int main(void) { // region_id: If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-1. std::string regionId = "<YOUR REGION ID>"; std::string projectId = "<YOUR PROJECTID>"; // ENDPOINT: On the console, choose Overview and click Access Addresses to view the HTTPS application access address. std::string endpoint = "<YOUR ENDPOINT>"; std::string ak; std::string sk; #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) ak = getenv("HUAWEICLOUD_SDK_AK"); sk = getenv("HUAWEICLOUD_SDK_SK"); #elif defined(linux) || defined(__linux) || defined(__linux__) char* envVar; #define INIT_ENV_VAR(ID, NAME) \ do { \ if (envVar = secure_getenv(#NAME)) { \ ID = std::string(envVar); \ } \ } while (0) INIT_ENV_VAR(ak, HUAWEICLOUD_SDK_AK); INIT_ENV_VAR(sk, HUAWEICLOUD_SDK_SK); #undef INIT_ENV_VAR #endif auto basicCredentials = std::make_unique<BasicCredentials>(); // Use the derived algorithm for the standard and enterprise editions. Delete the code for the basic edition. basicCredentials->setDerivedPredicate(Constants::DEFAULT_DERIVED_PREDICATE); basicCredentials->withAk(ak) .withSk(sk) .withProjectId(projectId); auto client = HuaweiCloud::Sdk::Iotda::V5::IoTDAClient::newBuilder() .withCredentials(std::unique_ptr<Credentials>(basicCredentials.release())) .withRegion(Region(regionId, endpoint)) .build(); // Initialize request parameters HuaweiCloud::Sdk::Iotda::V5::Model::ListDevicesRequest request = HuaweiCloud::Sdk::Iotda::V5::Model::ListDevicesRequest(); try { std::string stringValue; std::cout << "************ListDevice***********" << std::endl; // Call the API for querying the device list. std::shared_ptr<HuaweiCloud::Sdk::Iotda::V5::Model::ListDevicesResponse> listRes = client->listDevices(request); std::cout << listRes->getHttpBody() << std::endl; } catch (HostUnreachableException& e) { // handle exception std::cout << e.what() << std::endl; } catch (SslHandShakeException& e) { std::cout << e.what() << std::endl; } catch (RetryOutageException& e) { std::cout << e.what() << std::endl; } catch (CallTimeoutException& e) { std::cout << e.what() << std::endl; } catch (ServiceResponseException& e) { std::cout << "StatusCode: " << e.getStatusCode() << std::endl; std::cout << "ErrorCode: " << e.getErrorCode() << std::endl; std::cout << "ErrorMsg: " << e.getErrorMsg() << std::endl; std::cout << "RequestId: " << e.getRequestId() << std::endl; } return 0; } - If you are running the code on Linux, copy the above file to iotda_test.cpp. Run the following commands:
$ g++ -o iotda_test iotda_test.cpp --std=c++14 -liotda_v5 -lcore -lcrypto -lboost_system -lcpprest $ ./iotda_test # The actual running result is displayed below $.
If you use CMake to manage projects on Windows, you need to import the dependencies of the SDK Core package and service package to CMakeLists.txt. You can refer to the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.16) project(demo) find_package(CURL REQUIRED) set(CMAKE_CXX_STANDARD 14) set(LINK_DIR "C:/Program Files (x86)/huaweicloud_cpp_sdk_v3/bin;") set(BIN_DIR "C:/Program Files (x86)/huaweicloud_cpp_sdk_v3/lib;") set(SERVICE_DIR "C:/Program Files (x86)/huaweicloud_cpp_sdk_v3/include;") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBOOST_UUID_FORCE_AUTO_LINK") link_directories(${BIN_DIR}) include_directories(${SERVICE_DIR}) add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>") add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>") add_executable(demo main.cpp) target_link_libraries(demo PUBLIC core iotda_v5)
Additional Information
For details on the project source code and usage guide, see Huawei Cloud C++ Software Development Kit (C++ SDK).
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot