You can return to the original look by selecting English in the language selector
above.

C++ Sample for AWS Cloud9

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

Creating this sample might 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.

Prerequisites

Before you use this sample, be sure to meet the following requirements.

You must have an existing AWS Cloud9 EC2 development environment. This sample
assumes you already have an EC2 environment that is connected to an Amazon EC2 instance
running Amazon Linux
or Ubuntu Server. If you have a different type of environment or operating system,
you might
need to adapt this sample's instructions to set up related tools. See Creating an Environment in AWS Cloud9 for details.

You have the AWS Cloud9 IDE for the existing environment already open. When you open an environment, AWS Cloud9 opens the IDE for that environment in your
web browser.
See Opening an Environment in AWS Cloud9 for details.

Step 1: Install Required Tools

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.

Run the yum update command (on Amazon Linux) or the apt update command (on Ubuntu Server) to help ensure the latest security updates and
bug fixes are installed.

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.

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

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

wget https://github.com/aws/aws-sdk-cpp/archive/master.zip

Run the unzip command, specifying the name of the .zip file you just downloaded.

unzip master.zip

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

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 and
how much of the AWS SDK for C++ you choose to build.

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

mkdir sdk_build

Switch to the folder you just created.

cd sdk_build

Prepare to build the AWS SDK for C++ into this folder.

For Amazon Linux:

cmake3 ../aws-sdk-cpp-master

For Ubuntu Server:

cmake ../aws-sdk-cpp-master

Note

To build only the Amazon S3 portion of the AWS SDK for C++ and its dependencies, run
this command instead:

For Amazon Linux:

cmake3 ../aws-sdk-cpp-master -DBUILD_ONLY="s3"

For Ubuntu Server:

cmake ../aws-sdk-cpp-master -DBUILD_ONLY="s3"

Build the AWS SDK for C++ into this folder.

make

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

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.

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.

# 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

In the terminal, prepare to build your source code.

For Amazon Linux:

cmake3 -Daws-sdk-cpp_DIR=sdk_build .

For Ubuntu Server:

cmake -Daws-sdk-cpp_DIR=sdk_build .

Build your source code.

make

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

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

For Command, type s3-demo my-test-bucket us-east-2, where my-test-bucket is the name of the bucket you want to create
and then delete, and us-east-2 is the ID of the AWS Region you want to create the bucket in.
For more IDs, see Amazon Simple Storage Service (Amazon S3) in the Amazon Web Services General Reference.

Note

Amazon S3 bucket names must be unique across AWS—not just your AWS account.

Choose the Run button, and compare your output.

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: