How to edit aws s3 provided Aws-sdk-cpp CMakeLists.txt example to include other header files

  amazon-s3, amazon-web-services, c++, cmake

I have been using the Aws-sdk-cpp for s3 and had a quick question as to how I could edit the provided CMakeLists.txt file that is provided by aws (https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/cpp/example_code/s3/CMakeLists.txt) to be able to include other headers as I keep running into undefined reference errors.

Based off of the provided CMakeLists.txt file it will build any .cpp in the same folder as it into an executable, however what I want to do is be able to write a single main, where I could call functions in each of the following .cpp files instead of each .cpp becoming its own executable.(https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/cpp/example_code/s3). For example, in create_bucket.cpp I changed the code to just have the function and wrote a header file as well. I’ve also edited the CMakeLists.txt file to just build the main.cpp, however I receive the following error

CMakeFiles/main.dir/main2.cpp.o: In function `main':
`main2.cpp:(.text+0x3a6): undefined reference to `AwsDoc::S3::CreateBucket(Aws::S3::S3Client 
const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > 
const&)'
collect2: error: ld returned 1 exit status

I’m just confused as I included the create_bucket.h in the same folder as the other headers and I keep getting an undefined reference?

Thank you!

Here is my file structure.

main
--bld
--src
----CMakeFiles
----include
------awsdoc
--------s3
----------create_bucket.h
----------s3_examples.h
----------S3_EXPORTS.h
----------s3_list_objects_with_aws_global_region.h
----------s3-demo.h
----CMakeLists.txt
----create_bucket.cpp
----main2.cpp

The following is my code.
CMakeLists.txt

cmake_minimum_required(VERSION 3.3)

#give the project a name
project("aws")

#set the c++ standard
set(CMAKE_CXX_STANDARD_11)

# Build shared libraries by default.
if(NOT BUILD_SHARED_LIBS)
    set(BUILD_SHARED_LIBS ON)
endif()

# Find the AWS SDK for C++ package.
find_package(AWSSDK REQUIRED COMPONENTS s3 sts)

# If the compiler is some version of Microsoft Visual C++, or another compiler simulating C++, 
# and building as shared libraries, then dynamically link to those shared libraries.
if(MSVC AND BUILD_SHARED_LIBS)
    add_definitions(-DUSE_IMPORT_EXPORT)
    # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging.
    list(APPEND SERVICE_LIST s3 sts)
    #For IDE's like Xcode and Visual Studio this line will be ignored because Release/Debug 
    #  is switched internally, but this is necessary for non-IDE builds.
    set(CMAKE_BUILD_TYPE Debug) #TODO: Set to your build type
    
    #TODO:Choose appropriate one of the following two lines, you want to copy to the same folder where your executables are.
    AWSSDK_CPY_DYN_LIBS(SERVICE_LIST "" ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE})  #Choose this line if your executables are in /build/Debug
    #AWSSDK_CPY_DYN_LIBS(SERVICE_LIST "" ${CMAKE_CURRENT_BINARY_DIR})  #Choose this line for Visual Studio and possibly other IDEs
    
    message(STATUS ">>CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}")
    message(STATUS ">>CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
endif()


# Add the code example-specific header files.
file(GLOB AWSDOC_S3_HEADERS
    "include/awsdoc/s3/*.h"
)

# Add the code example-specific source files.
file(GLOB AWSDOC_S3_SOURCE
    "*.cpp"
)

add_executable(main main2.cpp)


target_include_directories(main PUBLIC 
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>)

target_link_libraries(main ${AWSSDK_LINK_LIBRARIES} 
    ${AWSSDK_PLATFORM_DEPS})

main2.cpp

#include <iostream>
#include <string>
#include <iostream>
#include <aws/core/Aws.h> //createbucket
#include <aws/s3/S3Client.h> //createbucket
#include <aws/s3/model/CreateBucketRequest.h> //createbucket
#include <aws/s3/model/BucketLocationConstraint.h> //createbucket
#include <aws/core/utils/UUID.h> //createbucket
#include <aws/core/utils/StringUtils.h> //createbucket
#include <awsdoc/s3/s3_examples.h> //createbucket
#include <awsdoc/s3/create_bucket.h>

int main(){
    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {   

        
        std::string proxyhost;
        int proxyport;
        int scheme_identifier;
        Aws::String config_region;
        Aws::Http::Scheme scheme;

// get the configurations from the user and set it up//

        std::cout << "enter in configurations" << "n";
        std::cout << "Enter proxyhost" << "n";
        std::cin >> proxyhost;
        std::cout << "n";
        std::cout << "Enter proxyport" << "n";
        std::cin >> proxyport;
        std::cout << "n";
        std::cout << "would you like to use Http or Https for your Http scheme? enter 0 for Http and 1 for Https" << "n";
        std::cin >> scheme_identifier;
        std::cout << "n";
        std::cout << "Enter region" << "n";
        std::cin >> config_region;

        std::cout << "would you like to use Http or Https for your Http scheme? enter 0 for Http and 1 for Https" << "n";
        std::cin >> scheme_identifier;

        Aws::Client::ClientConfiguration clientConfig;
        clientConfig.proxyHost = proxyhost;
        clientConfig.proxyPort = proxyport;
        clientConfig.proxyScheme = scheme;
        clientConfig.region = config_region;
        Aws::S3::S3Client s3_client(clientConfig);

        Aws::String object_name;
        Aws::String bucket_name;
        Aws::String bucket;
        Aws::String region;
        Aws::String object;

        std::cout << "Enter bucket name, remember bucket name rules" << "n";
        std::cin >> bucket;
        std::cout << "n";
        std::cout << "Enter name of file, must be in the build directory" << "n";
        std::cin >> object;
        std::cout << "n";

        bucket_name = bucket;
        object_name = object;

        AwsDoc::S3::CreateBucket(s3_client, bucket_name);
    }
}

create_bucket.cpp

#include <iostream>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>
#include <aws/s3/model/BucketLocationConstraint.h>
#include <aws/core/utils/UUID.h>
#include <aws/core/utils/StringUtils.h>
#include <awsdoc/s3/s3_examples.h>
#include <awsdoc/s3/create_bucket.h>


// snippet-end:[s3.cpp.create_bucket.inc]

/* ////////////////////////////////////////////////////////////////////////////
 * Purpose: Creates a bucket in Amazon S3.
 *
 * Inputs:
 * - bucketName: The name of the bucket to create. 
 * - region: The AWS Region to create the bucket in.
 * 
 * Outputs: true if the bucket was created; otherwise, false.
 * ///////////////////////////////////////////////////////////////////////// */

// snippet-start:[s3.cpp.create_bucket.code]
bool AwsDoc::S3::CreateBucket(const Aws::S3::S3Client& s3Client,
        const Aws::String& bucketName){

    Aws::S3::Model::CreateBucketRequest request;
    request.SetBucket(bucketName);


    Aws::S3::Model::CreateBucketOutcome outcome = 
    s3Client.CreateBucket(request);

    if (!outcome.IsSuccess())
    {
        auto err = outcome.GetError();
        std::cout << "Error: CreateBucket: " <<
            err.GetExceptionName() << ": " << err.GetMessage() << std::endl;

        return false;
    }

    return true;
}

create_bucket.h

bool AwsDoc::S3::CreateBucket(const Aws::S3::S3Client& s3Client,
        const Aws::String& bucketName);

Source: Windows Questions C++

LEAVE A COMMENT