MacOS 编译使用aliyun-openapi-cpp-sdk
2024-12-20 10:46:59 5
开发库
sdk依赖了几个库, 需要先安装. (brew: 包管理器)
brew install jsoncpp
brew install openssl
brew install ossp-uuid
brew install curl
源码
源码下载下来
git clone https://github.com/aliyun/aliyun-openapi-cpp-sdk.git
修改CMake
我这里要使用domain相关api, 所以这里以domain为例
不同于yum和apt, brew安装开发库会放在/opt/homebrew/Cellar这个路径下, 所以项目默认是找不到我们安装的库的, 需要修改cmake, 指定路径
core/CMakeLists.txt
else()
target_include_directories(core
PRIVATE /usr/include/jsoncpp)
target_link_libraries(core
crypto
curl
jsoncpp
uuid )
endif()
修改为
else()
target_include_directories(core
PRIVATE /opt/homebrew/Cellar/jsoncpp/1.9.6/include
PRIVATE /opt/homebrew/Cellar/openssl@3/3.4.0/include
)
target_link_libraries(core
/opt/homebrew/opt/openssl@3/lib/libcrypto.dylib
curl
/opt/homebrew/Cellar/jsoncpp/1.9.6/lib/libjsoncpp.dylib
/opt/homebrew/Cellar/ossp-uuid/1.6.2_2/lib/libuuid.dylib )
endif()
domain/CMakeLists.txt
原先
else()
target_include_directories(domain
PRIVATE /usr/include/jsoncpp)
target_link_libraries(domain
jsoncpp)
endif()
修改为
else()
target_include_directories(domain
PRIVATE /opt/homebrew/Cellar/jsoncpp/1.9.6/include)
target_link_libraries(domain
/opt/homebrew/Cellar/jsoncpp/1.9.6/lib/libjsoncpp.dylib)
endif()
编译
mkdir sdk_build && cd sdk_build
## 开发就先用Debug 指定BUILD_PRODUCT为core
sudo cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_PRODUCT=core ..
sudo make
编译完后sdk_build/lib
目录下就有libalibabacloud-sdk-core.dylib
了, domain模块也是一样的步骤
使用
.
├── CMakeLists.txt
├── main.cpp
└── third
├── include
│ └── alibabacloud
│ ├── core/
│ └── domain/
└── lib
├── libalibabacloud-sdk-core.dylib
└── libalibabacloud-sdk-domain.dylib
CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
project(domain_checker)
set(CMAKE_CXX_STANDARD 17)
include_directories(${CMAKE_SOURCE_DIR}/third/include)
file(GLOB THIRD_LIB "${CMAKE_SOURCE_DIR}/third/lib/*.*")
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${THIRD_LIB})
#include <cstdlib>
#include <iostream>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/core/CommonRequest.h>
#include <alibabacloud/core/CommonClient.h>
#include <alibabacloud/core/CommonResponse.h>
using namespace std;
int main(int argc, char **argv) {
AlibabaCloud::InitializeSdk();
AlibabaCloud::ClientConfiguration configuration("cn-hangzhou");
// specify timeout when create client.
configuration.setConnectTimeout(1500);
configuration.setReadTimeout(4000);
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
AlibabaCloud::Credentials credential("xxx", "xxx");
/* use STS Token
credential.setSessionToken( getenv("ALIBABA_CLOUD_SECURITY_TOKEN") );
*/
AlibabaCloud::CommonClient client(credential, configuration);
AlibabaCloud::CommonRequest request(AlibabaCloud::CommonRequest::RequestPattern::RpcPattern);
request.setHttpMethod(AlibabaCloud::HttpRequest::Method::Post);
request.setDomain("domain.aliyuncs.com");
request.setVersion("2018-01-29");
request.setQueryParameter("Action", "CheckDomain");
request.setQueryParameter("DomainName", "22xcode.com");
auto response = client.commonResponse(request);
if (response.isSuccess()) {
printf("request success.\n");
printf("result: %s\n", response.result().payload().c_str());
} else {
printf("error: %s\n", response.error().errorMessage().c_str());
printf("request id: %s\n", response.error().requestId().c_str());
}
AlibabaCloud::ShutdownSdk();
return 0;
}
输出
request success.
result: {"RequestId":"xxx-xxx-xx-xx-xxx","Avail":0,"DomainName":"22xcode.com","Premium":false,"DynamicCheck":false,"Reason":"Domain exists"}