0

I am currently using the AWS CPP SDK for s3 and I am trying to call functions in my C++ files from a C file.

I have already looked up guides on how to call C++ functions from a C file, and I have done so successfully with simple, non-AWS functions. However, when I try and use the same guide to do so with an AWS CPP SDK function it does not work correctly. Below are the files and the commands I am running.

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

To run list_buckets.cpp, I use g++ -std=c++17 -Wall -laws-cpp-sdk-core -laws-cpp-sdk-s3 list_buckets.cpp -o list_buckets && ./list_buckets, and the output is:

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

To build the library files for listing the AWS buckets, I run g++ -std=c++17 -laws-cpp-sdk-core -laws-cpp-sdk-s3 list_buckets.cpp -shared -o liblist_buckets.so.

To run list_buckets.c, I run gcc -I/Library/Developer/CommandLineTools/usr/include/c++/v1 -L. -llist_buckets list_buckets.c. However, it only produces errors which I have provided below.

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

I am stuck on how to resolve this issue. Any and all help/guidance is greatly appreciated.

dmoini
  • 313
  • 2
  • 15

1 Answers1

0

Change list_buckets.h

#include <aws/core/Aws.h>

extern "C" {
void listBuckets();
}

This tells the C compiler what the linking syntax is. This affects the function naming scheme that the linker uses to resolve symbols.

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • I still get the same errors as before. – dmoini Jul 01 '19 at 22:15
  • Make sure that you are including the same header file in all modules that reference or define the functions accessed from C. This means both C and C++ source files. I do this all the time, so you have a simple mistake somewhere. – John Hanley Jul 01 '19 at 22:20