【问题标题】:How to call AWS CPP SDK functions from C [duplicate]如何从 C 调用 AWS CPP SDK 函数 [重复]
【发布时间】:2019-07-01 21:46:59
【问题描述】:

我目前正在使用适用于 s3 的 AWS CPP 开发工具包,并且我正在尝试从 C 文件调用我的 C++ 文件中的函数。

我已经查阅了有关如何从 C 文件调用 C++ 函数的指南,并且我已经使用简单的非 AWS 函数成功地做到了这一点。但是,当我尝试使用相同的指南使用 AWS CPP 开发工具包功能时,它无法正常工作。以下是我正在运行的文件和命令。

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 &amp;&amp; ./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

我被困在如何解决这个问题上。非常感谢任何和所有帮助/指导。

【问题讨论】:

  • 您在 C 代码中包含 C++ 特定标头。那是行不通的。
  • @1201ProgramAlarm 在不使用 C++ 标头的情况下如何解决我的问题?
  • 通常您会创建 C 代码可以调用的 C 包装函数和结构(在头文件中声明,在 cpp 文件中定义,均以extern "C" 形式)。

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


【解决方案1】:

更改 list_buckets.h

#include <aws/core/Aws.h>

extern "C" {
void listBuckets();
}

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

【讨论】:

  • 我仍然遇到和以前一样的错误。
  • 确保在所有引用或定义从 C 访问的函数的模块中包含相同的头文件。这意味着 C 和 C++ 源文件。我一直这样做,所以你在某个地方有一个简单的错误。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多