Posted on

Accessing AWS (s3) from C++ under CMake

I wanted to start using the AWS SDK from a native app with the least amount of friction to simplify porting it to other platforms and thought about sharing the approach in case it’s of use to others.


Whilst Amazon provide a CMake find module to locate the AWS SDK’s libs and headers those libs and headers need to be installed on the system and platform-specific steps to do that are described (at the time of writing) in the AWS docs as a precursor to finding it using CMake. The process however could be simplified by instead performing the SDK build and installation within CMake itself as part of your app’s project using CMake’s ExternalProject support. By performing the initial SDK fetch and install in CMake you can trivialise the move to other platforms given the entire app build (app + SDK) is now managed by CMake. Potentially the Amazon provided CMake find module could also be discarded (as we control what we are building and where we are building it to – i.e. a project specific build directory that we control as part of our project’s CMake file vs. the entire system).

Integrating the AWS SDK (s3) with CMake

The snippet below will download, build and then link in the minimum required AWS SDK libraries to perform S3 access in C++ from an app using this approach. The CMake code below describes an app (example_AWS) that has a single source file (example.cpp) the source code of which is left as an exercise for the reader (there are plenty of s3 examples in Amazon’s SDK documentation linked to below).


# Build a minimal AWS SDK for s3
    PREFIX              ${CMAKE_BINARY_DIR}/aws-sdk
    UPDATE_COMMAND      ""
ExternalProject_Get_Property(aws-sdk INSTALL_DIR)
add_library(aws-sdk::core SHARED IMPORTED)
add_library(aws-sdk::s3   SHARED IMPORTED)
set(AWS_LIBS         aws-sdk::s3 aws-sdk::core)
include_directories(SYSTEM ${AWS_INCLUDE_DIRS})

# Build our project (that uses s3)


set(PROJECT_NAME example_AWS)
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
add_dependencies(${PROJECT_NAME} aws-sdk)
target_link_libraries(${PROJECT_NAME} ${AWS_LIBS})
    set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH "@executable_path")

NB. If you’re cross-compiling then you’ll need to provide the CMAKE_TOOLCHAIN_FILE and any toolchain specific arguments to the CONFIGURE_COMMAND above (i.e. CMAKE_TOOLCHAIN_FILE=path/to/my/tool_chain_file.cmake). 

Accessing s3

From here you can now follow the S3 examples provided by Amazon:

Amazon S3 code examples and S3 – encrypting data prior to sending to S3



Liked it? Take a second to support ben morris on Patreon!