如何从C中调用AWS CPP SDK函数

How to call AWS CPP SDK functions from C

本文关键字:AWS CPP SDK 函数 调用      更新时间:2023-10-16

我当前正在使用S3的AWS CPP SDK,我正在尝试从C文件中调用C 文件中的函数。

我已经查找了如何从C文件调用C 功能的指南,并且我通过简单的非AWS函数成功地完成了指南。但是,当我尝试使用相同的指南使用AWS CPP SDK功能进行同样的指南时,它无法正常工作。以下是我正在运行的文件和命令。

list_buckets.cpp

#include <stdio.h>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/Bucket.h>
using namespace std;
extern "C" {
    void listBuckets();
}
void listBuckets() {
    Aws::S3::S3Client s3_client;
    auto outcome = s3_client.ListBuckets();
    if (outcome.IsSuccess()) {
       cout << "Your Amazon S3 buckets:" << endl;
       Aws::Vector<Aws::S3::Model::Bucket> bucket_list = outcome.GetResult().GetBuckets();
       for (auto const &bucket : bucket_list) {
          cout << "  * " << bucket.GetName() << endl;
       }
    } else {
       cout << "ListBuckets error: " << outcome.GetError().GetExceptionName() << " - " << outcome.GetError().GetMessage() << endl;
    }
}
int main(int argc, char const *argv[]) {
    Aws::SDKOptions options;
    Aws::InitAPI(options);
    cout << "Listing buckets from list_buckets.cpp" << endl;
    listBuckets();
    Aws::ShutdownAPI(options);
    return 0;
}

list_buckets.h

#include <aws/core/Aws.h>
void listBuckets();

list_buckets.c

#include "list_buckets.h"
int main(int argc, char const *argv[]) {
    Aws::SDKOptions options;
    Aws::InitAPI(options);
    printf("Listing buckets from list_buckets.cn");
    listBuckets();
    Aws::ShutdownAPI(options);
    return 0;
}

要运行list_buckets.cpp,我使用 g++ -std=c++17 -Wall -laws-cpp-sdk-core -laws-cpp-sdk-s3 list_buckets.cpp -o list_buckets && ./list_buckets,输出为:

Your Amazon S3 buckets:
  * bucket-name1
  * bucket-name2
  * bucket-name3

要构建用于列出AWS存储桶的库文件,我运行g++ -std=c++17 -laws-cpp-sdk-core -laws-cpp-sdk-s3 list_buckets.cpp -shared -o liblist_buckets.so

要运行list_buckets.c,我运行 gcc -I/Library/Developer/CommandLineTools/usr/include/c++/v1 -L. -llist_buckets list_buckets.c。但是,它仅产生我在下面提供的错误。

In file included from list_buckets.c:1:
In file included from ./list_buckets.h:1:
In file included from /usr/local/include/aws/core/Aws.h:17:
In file included from /usr/local/include/aws/core/utils/logging/LogLevel.h:20:
In file included from /usr/local/include/aws/core/utils/memory/stl/AWSString.h:20:
In file included from /usr/local/include/aws/core/utils/memory/stl/AWSAllocator.h:21:
In file included from /usr/local/include/aws/core/utils/memory/AWSMemory.h:20:
In file included from /usr/local/include/aws/core/utils/memory/MemorySystemInterface.h:20:
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:98:1: error: unknown type name '_LIBCPP_BEGIN_NAMESPACE_STD'
_LIBCPP_BEGIN_NAMESPACE_STD
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:100:7: error: expected ';' after top level declarator
using ::size_t;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:100:8: error: expected identifier or '('
using ::size_t;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:101:7: error: expected ';' after top level declarator
using ::div_t;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:101:8: error: expected identifier or '('
using ::div_t;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:102:7: error: expected ';' after top level declarator
using ::ldiv_t;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:102:8: error: expected identifier or '('
using ::ldiv_t;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:104:7: error: expected ';' after top level declarator
using ::lldiv_t;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:104:8: error: expected identifier or '('
using ::lldiv_t;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:106:7: error: expected ';' after top level declarator
using ::atof;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:106:8: error: expected identifier or '('
using ::atof;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:107:7: error: expected ';' after top level declarator
using ::atoi;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:107:8: error: expected identifier or '('
using ::atoi;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:108:7: error: expected ';' after top level declarator
using ::atol;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:108:8: error: expected identifier or '('
using ::atol;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:110:7: error: expected ';' after top level declarator
using ::atoll;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:110:8: error: expected identifier or '('
using ::atoll;
       ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:112:7: error: expected ';' after top level declarator
using ::strtod;
      ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib:112:8: error: expected identifier or '('
using ::strtod;
       ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [executeListBucketsLib] Error 1

我被困在如何解决此问题上。非常感谢任何帮助/指导。

更改list_buckets.h

#include <aws/core/Aws.h>
extern "C" {
void listBuckets();
}

这告诉C编译器链接语法是什么。这会影响链接器用来解决符号的功能命名方案。