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"}


MacOS 编译使用aliyun-openapi-cpp-sdk

开发库sdk依赖了几个库, 需要先安装. (brew: 包管理器)brew install jsoncpp brew install openssl brew install ossp-uuid brew install curl源码源码下载下来git clone https://github.co
2024-12-20

AWS SES SDK V2 API 邮件发送

记录一下@Bean public SesClient sesClient(EmailAwsConfig config) { AwsBasicCredentials build = AwsBasicCredentials.builder() .accountId(con
2024-11-08

freemarker 时间显示不正常 设置时区

项目在本地开发的时候显示正常,部署上服务器就一直差8个小时,最后发现freemarker官方文档有这样的说明time_zone:时区的名称来显示并格式化时间。 默认情况下,使用JVM的时区。 也可以是 Java 时区 API 接受的值,或者 "JVM default" (从 FreeMarker 2
2020-03-28
IDEA 2019.1 xml 不高亮

IDEA 2019.1 xml 不高亮

前几天更新了idea后,发现xml里的代码都没有了高亮,变得跟记事本一个德性了打开setting ,搜索 File Types,找到xml项, 查看下方的匹配格式,果然没有xml,(idea真是厉害)点击右方的+,输入*.xml,点击ok,解决问题
2020-03-28

npm install 淘宝镜像

npm install --registry=https://registry.npm.taobao.org
2020-03-28
Java中方法的参数传递机制

Java中方法的参数传递机制

来看一段代码 public class Man { private String name; private Integer age; public String getName() { return name; } publi
2020-03-28
基于自定义注解手写权限控制

基于自定义注解手写权限控制

方法一: AOP 方法二: 拦截器项目结构项目依赖<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-w
2020-03-28

Docker 部署 详细全过程 附代码

Docker 部署本站 全过程环境:CentOS7.61. 安装Docker其他版本CentOS可以参考这个https://help.aliyun.com/document_detail/187598.html查看本机内核版本,内核版本需高于 3.10uname -r 确保 yum 包最新yum u
2020-03-28

SpringBoot 启动普通java工程

引入依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.9</version> </dependency>
2020-03-28

Vue.js DOM操作

<template> <input type="button" @click="reply($event)" value="回复"> </template> export default { methods: { replyFun(e) {
2020-03-29
CentOS7编译调试OpenJDK12

CentOS7编译调试OpenJDK12

1. 下载源码https://hg.openjdk.java.net/jdk/jdk12点击左侧的browse,再点击zip,就可以下载zip格式的源码压缩包。unzip xxx.zip 解压文件2. 安装jdkyum install java-11-openjdk-devel -y3. 运行con
2020-04-23
编写自己的Spring Boot Starter

编写自己的Spring Boot Starter

1.新建一个maven项目命名规则统一是xxx-spring-boot-starter完整pom.xml<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"
2020-06-29