Menu
AWS Cloud9
User Guide

C++ Sample for AWS Cloud9

This sample enables you to run some C++ code in an AWS Cloud9 development environment.

Note

This sample assumes you already have the AWS Cloud9 IDE for your AWS Cloud9 development environment open in your web browser, and that you're using an AWS Cloud9 EC2 development environment that is connected to an Amazon EC2 instance running Amazon Linux. In you're using a different operating system or using an AWS Cloud9 SSH development environment, you might need to adapt this sample's instructions to correctly install and configure this sample's required tools. To create an environment, see Creating an Environment.

When you're using this sample, be sure you're signed in to AWS with the AWS account ID and name and password of the user you created or identified in Team Setup.

Creating this sample may result in charges to your AWS account. These include possible charges for services such as Amazon EC2 and Amazon S3. For more information, see Amazon EC2 Pricing and Amazon S3 Pricing.

Step 1: Install Required Tools

In this step, you install and configure the GNU Complier Collection (GCC), which is required to run this sample.

  1. In a terminal session in the AWS Cloud9 IDE, confirm whether GCC is already installed by running the g++ --version command. (To start a new terminal session, on the menu bar, choose Window, New Terminal.) If successful, the output contains the GCC version number. Otherwise, an error message should be output. If GCC is installed, skip ahead to Step 2: Add Code.

  2. Run the yum update command to help ensure the latest security updates and bug fixes are installed.

    Copy
    sudo yum -y update
  3. To install GCC, run the yum install command.

    Copy
    sudo yum -y install gcc-c++
  4. Confirm that GCC is now successfully installed by running the g++ --version command. If successful, the output contains the GCC version number.

Step 2: Add Code

In the AWS Cloud9 IDE, create a file with this content, and save the file with the name hello.cpp. (To create a file, on the menu bar, choose File, New File. To save the file, choose File, Save.)

Copy
#include <iostream> #include <stdlib.h> int main( int argc, char *argv[] ) { std::cout << "Hello, World!\n"; std::cout << "The sum of 2 and 3 is 5.\n"; if (argc > 2) { std::cout << "The sum of " << argv[1] << " and " << argv[2] << " is " << atoi(argv[1]) + atoi(argv[2]) << ".\n"; } return 0; }

Step 3: Run the Code

  1. Compile the hello.cpp source code into an object module, and then link the object module into a program named hello. Do this by choosing Run, Build System, G++ followed by Run, Build on the menu bar.

    Note

    If G++ is not available, you can create a custom builder for G++.

    1. Choose Run, Build System, New Build System on the menu bar.

    2. On the My Builder.build tab, replace the tab's contents with this code.

      Copy
      { "cmd": [ "g++", "-o", "$file_base_name", "$file_name" ], "info": "Compiling $file_name and linking to $file_base_name...", "selector": "source.cpp" }
    3. Choose File, Save As on the menu bar, and then save the file as G++.build in the /.c9/builders folder.

    4. Choose the hello.cpp tab to make it active.

    5. Choose Run, Build System, G++ followed by Run, Build.

  2. In the AWS Cloud9 IDE, run the code by choosing Run, Run Configurations, New Run Configuration on the menu bar.

  3. On the [New] - Idle tab, choose Runner: Auto, and then choose C++.

    Note

    If C++ isn't available, you can create a custom runner for C++.

    1. On the [New] - Idle tab, choose Runner: Auto, and then choose New Runner.

    2. On the My Runner.run tab, replace the tab's contents with this code.

      Copy
      { "cmd" : ["$file", "$args"], "info" : "Running $project_path$file_name...", "selector" : "source" }
    3. Choose File, Save As on the menu bar, and then save the file as C++.run in the /.c9/runners folder.

    4. On the [New] - Idle tab, choose Runner: Auto, and then choose C++.

  4. For Command, type hello 5 9. In the code, 5 represents argv[1], and 9 represents argv[2].

  5. Choose the Run button, and compare your output.

    Copy
    Hello, World! The sum of 2 and 3 is 5. The sum of 5 and 9 is 14.

Step 4: Install and Configure the AWS SDK for C++

You can enhance this sample to use the AWS SDK for C++ to create an Amazon S3 bucket, list your available buckets, and then delete the bucket you just created.

In this step, you install and configure the AWS SDK for C++, which provides a convenient way to interact with AWS services, such as Amazon S3, from your C++ code. Before you install the AWS SDK for C++, you must install some dependencies. After you install the AWS SDK for C++, you must set up credentials management in your environment. The AWS SDK for C++ needs these credentials to interact with AWS services.

Note

The following steps require your environment to be running on an Amazon EC2 instance or your own server that has at least 4 GB of RAM.

To install AWS SDK for C++ dependencies

From a terminal session in the AWS Cloud9 IDE, run the following command to install several packages that the AWS SDK for C++ depends on to run correctly.

Copy
sudo yum -y install libcurl-devel openssl-devel libuuid-devel cmake3

To download and extract the AWS SDK for C++ source code

  1. Run the wget command, specifying the location of the AWS SDK for C++ source.

    Copy
    wget https://github.com/aws/aws-sdk-cpp/archive/master.zip
  2. Run the unzip command, specifying the name of the .zip file you just downloaded.

    Copy
    unzip master.zip
  3. Run the rm command to delete the .zip file, as you no longer need it.

    Copy
    rm master.zip

To build the AWS SDK for C++

Note

This step could take up to one or more hours to complete, depending on the computing resources available to your Amazon EC2 instance or your own server.

  1. Create a folder to build the AWS SDK for C++ into.

    Copy
    mkdir sdk_build
  2. Switch to the folder you just created.

    Copy
    cd sdk_build
  3. Prepare to build the AWS SDK for C++ into this folder.

    Copy
    cmake3 ../aws-sdk-cpp-master
  4. Build the AWS SDK for C++ into this folder.

    Copy
    make
  5. After the AWS SDK for C++ successfully builds, switch to the root of your environment.

    Copy
    cd ..

To set up credentials management in your environment

Each time you use the AWS SDK for C++ to call an AWS service, you must provide a set of credentials with the call. These credentials determine whether the AWS SDK for C++ has the appropriate permissions to make that call. If the credentials don't cover the appropriate permissions, the call will fail.

In this step, you store your credentials within the environment. To do this, follow the instructions in Call AWS Services from an Environment, and then return to this topic.

For additional information, see Providing AWS Credentials in the AWS SDK for C++ Developer Guide.

Step 5: Add AWS SDK Code

In this step, you add some more code, this time to interact with Amazon S3 to create a bucket, list your available buckets, and then delete the bucket you just created. You will run this code later.

  1. In the AWS Cloud9 IDE, create a file with this content, and save the file with the name s3.cpp at the root (/) of your environment.

    Copy
    #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/Bucket.h> #include <aws/s3/model/CreateBucketRequest.h> #include <aws/s3/model/DeleteBucketRequest.h> bool ListMyBuckets(Aws::S3::S3Client s3_client); bool CreateMyBucket(Aws::S3::S3Client s3_client, Aws::String bucket_name); bool DeleteMyBucket(Aws::S3::S3Client s3_client, Aws::String bucket_name); void Cleanup(Aws::SDKOptions options); int main(int argc, char** argv) { Aws::String bucket_name = argv[1]; Aws::SDKOptions options; Aws::InitAPI(options); { Aws::S3::S3Client s3_client; if (!ListMyBuckets(s3_client)) { Cleanup(options); } if (!CreateMyBucket(s3_client, bucket_name)) { Cleanup(options); } if (!ListMyBuckets(s3_client)) { Cleanup(options); } if (!DeleteMyBucket(s3_client, bucket_name)) { Cleanup(options); } if (!ListMyBuckets(s3_client)) { Cleanup(options); } } Cleanup(options); } // List all of your available buckets in this AWS Region. bool ListMyBuckets(Aws::S3::S3Client s3_client) { auto outcome = s3_client.ListBuckets(); if (outcome.IsSuccess()) { std::cout << "My buckets now are:" << std::endl << std::endl; Aws::Vector<Aws::S3::Model::Bucket> bucket_list = outcome.GetResult().GetBuckets(); for (auto const &bucket: bucket_list) { std::cout << bucket.GetName() << std::endl; } std::cout << std::endl; return true; } else { std::cout << "ListBuckets error: " << outcome.GetError().GetExceptionName() << std::endl; << outcome.GetError().GetMessage() << std::endl; return false; } } // Create a bucket in this AWS Region. bool CreateMyBucket(Aws::S3::S3Client s3_client, Aws::String bucket_name) { std::cout << "Creating a new bucket named '" << bucket_name << "'..." << std::endl << std::endl; Aws::S3::Model::CreateBucketRequest bucket_request; bucket_request.WithBucket(bucket_name); auto outcome = s3_client.CreateBucket(bucket_request); if (outcome.IsSuccess()) { return true; } else { std::cout << "CreateBucket error: " << outcome.GetError().GetExceptionName() << std::endl << outcome.GetError().GetMessage() << std::endl; return false; } } // Delete the bucket you just created. bool DeleteMyBucket(Aws::S3::S3Client s3_client, Aws::String bucket_name) { std::cout << "Deleting the bucket named '" << bucket_name << "'..." << std::endl << std::endl; Aws::S3::Model::DeleteBucketRequest bucket_request; bucket_request.WithBucket(bucket_name); auto outcome = s3_client.DeleteBucket(bucket_request); if (outcome.IsSuccess()) { return true; } else { std::cout << "DeleteBucket error: " << outcome.GetError().GetExceptionName() << std::endl << outcome.GetError().GetMessage() << std::endl; return false; } } void Cleanup(Aws::SDKOptions options) { Aws::ShutdownAPI(options); }
  2. Create a file with this content, and save the file with the name CMakeLists.txt at the root (/) of your environment. This file enables you to build your code into an executable file.

    Copy
    # A minimal CMakeLists.txt file for the AWS SDK for C++. # The minimum version of CMake that will work. cmake_minimum_required(VERSION 2.8) # The project name. project(s3-demo) # Locate the AWS SDK for C++ package. # Requires that you build with: # -Daws-sdk-cpp_DIR=/path/to/sdk_build # or export/set: # CMAKE_PREFIX_PATH=/path/to/sdk_build find_package(aws-sdk-cpp) # Link to the AWS SDK for C++ shared libraries. add_definitions(-DUSE_IMPORT_EXPORT) # The executable name and its source files. add_executable(s3-demo s3-demo.cpp) # The libraries used by your executable. target_link_libraries(s3-demo aws-cpp-sdk-s3)

Step 6: Build and Run the AWS SDK Code

  1. In the terminal, prepare to build your source code.

    Copy
    cmake -Daws-sdk-cpp_DIR=sdk_build .
  2. Build your source code.

    Copy
    make
  3. Run the code by choosing Run, Run Configurations, New Run Configuration on the menu bar.

  4. On the [New] - Idle tab, choose Runner: Auto, and then choose C++.

  5. For Command, type s3-demo YOUR-BUCKET-NAME , where YOUR-BUCKET-NAME is the name of the bucket you want to create and then delete.

  6. Choose the Run button, and compare your output.

    Copy
    My buckets now are: Creating a new bucket named 'my-test-bucket'... My buckets now are: my-test-bucket Deleting the bucket named 'my-test-bucket'... My buckets now are:

Step 7: Clean Up

To prevent ongoing charges to your AWS account after you're done using this sample, you should delete the environment. For instructions, see Deleting an Environment.