如何从C调用AWS CPP SDK函数

时间:2019-07-01 21:46:59

标签: c++ c amazon-web-services amazon-s3 aws-sdk

我当前正在使用适用于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.c\n");
    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

我对如何解决此问题感到困惑。任何帮助/指导都将不胜感激。

1 个答案:

答案 0 :(得分:0)

更改list_buckets.h

#include <aws/core/Aws.h>

extern "C" {
void listBuckets();
}

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