Building an AWS C++ S3 Program on Windows with vcpk
Building an AWS C++ S3 Program on Windows with vcpkg
This guide shows how to set up a working C++ project on Windows that uses the AWS SDK to list S3 buckets. We will use vcpkg as the package manager to handle the AWS SDK installation and dependencies.
Step 1: Install vcpkg
If you haven't installed vcpkg yet, download it from the official repository and bootstrap it:
git clone https://github.com/microsoft/vcpkg.git C:\Users\YourName\CDK\vcpkg
cd C:\Users\YourName\CDK\vcpkg
.\bootstrap-vcpkg.bat
Note: On Windows, you must run bootstrap-vcpkg.bat
in the Command Prompt.
Step 2: Install the AWS SDK for C++ using vcpkg
Install the core and S3 components for 64-bit Windows:
vcpkg install aws-sdk-cpp[core,s3]:x64-windows
This will install the required libraries and headers under installed\x64-windows\
. vcpkg will handle dependencies like aws-c-common
, aws-crt-cpp
, and others.
Step 3: Set up your C++ project
Create a project folder, e.g., C:\AWS_CPP_S3
, and add your main.cpp
:
#include <iostream>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/ListBucketsRequest.h>
int main() {
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::S3::S3Client s3_client;
auto outcome = s3_client.ListBuckets();
if (outcome.IsSuccess()) {
std::cout << "Buckets:\n";
for (auto const &bucket : outcome.GetResult().GetBuckets()) {
std::cout << " - " << bucket.GetName() << "\n";
}
} else {
std::cerr << "Error listing buckets: " << outcome.GetError().GetMessage() << "\n";
}
}
Aws::ShutdownAPI(options);
return 0;
}
---
Step 4: Create CMakeLists.txt
Use CMake to build your project and integrate with vcpkg. Here’s a working CMakeLists.txt for Windows:
cmake_minimum_required(VERSION 3.20)
project(HelloS3 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# vcpkg toolchain
set(CMAKE_TOOLCHAIN_FILE "C:/Users/YourName/CDK/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
# Find individual AWS SDK components
find_package(aws-cpp-sdk-core REQUIRED)
find_package(aws-cpp-sdk-s3 REQUIRED)
# Executable
add_executable(HelloS3 main.cpp)
# Include directories and link libraries
target_include_directories(HelloS3 PRIVATE
"C:/Users/YourName/CDK/vcpkg/installed/x64-windows/include"
)
target_link_libraries(HelloS3 PRIVATE aws-cpp-sdk-core aws-cpp-sdk-s3)
Note: Replace YourName
with your actual Windows username.
Step 5: Build the project using CMake
Open the x64 Native Tools Command Prompt for VS 2022, navigate to your project folder, and run:
cd C:\AWS_CPP_S3
rmdir /s /q build
mkdir build
cd build
cmake .. -G "Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE="C:/Users/YourName/CDK/vcpkg/scripts/buildsystems/vcpkg.cmake"
cmake --build . --config Release
This will generate a HelloS3.exe
in build\Release
.
Step 6: Run your program
Make sure your AWS credentials are configured (either in %USERPROFILE%\.aws\credentials
or environment variables). Then run:
Release\HelloS3.exe
You should see a list of your S3 buckets, confirming that your program can communicate with AWS S3.
---Troubleshooting Tips
- If CMake cannot find the AWS SDK targets, make sure you specify the vcpkg toolchain with
-DCMAKE_TOOLCHAIN_FILE
. - If you switch architectures (e.g., from Win32 to x64), delete the build folder to clear the CMake cache.
- On Windows, the vcpkg AWS SDK does not provide
AWSSDKConfig.cmake
— use the individual component targets likeaws-cpp-sdk-core
andaws-cpp-sdk-s3
. - Always build from the Visual Studio Developer Command Prompt to ensure the correct compiler environment.
Conclusion
Using vcpkg simplifies dependency management for AWS SDK C++ on Windows. Following these steps, you can compile, link, and run a working AWS S3 program with C++ and CMake. From here, you can expand to other AWS services by installing additional components through vcpkg.
Comments
Post a Comment