AWS CPP SDK 编译链接

本贴最后更新于 2419 天前,其中的信息可能已经时移世易

Hello

AWS scheme

modify date:2018.03.22

[TOC]

AWS 库编译

To use the AWS SDK for C++, you need:

  • Visual Studio 2013 or later

    ​ Note:Visual Studio 2013 doesn't provide default move constructors and operators. Later versions of Visual Studio provide a standards-compliant compiler.

  • or GNU Compiler Collection (GCC) 4.9 or later

  • or Clang 3.3 or later

  • A minimum of 4 GB of RAM

(-dev packages) for libcurl, libopenssl, libuuid, zlib

cmake version > 3.0

unix 编译依赖

  • libz
  • curl
  • openssl
  • libuuid

Ubuntu 安装开发库 sudo apt-get install libcurl4-openssl-dev libssl-dev uuid-dev zlib1g-dev libpulse-dev

openssl
uuid
zlib
curl

centos

安装新版本 gcc 和 gdb

# 1. Install a package with repository for your system:
# On CentOS, install package centos-release-scl available in CentOS repository:
$ sudo yum install centos-release-scl

# On RHEL, enable RHSCL repository for you system:
$ sudo yum-config-manager --enable rhel-server-rhscl-7-rpms

# 2. Install the collection:
$ sudo yum install devtoolset-7

# 3. Start using software collections:
$ scl enable devtoolset-7 bash

安装 cmake

yum install llvm-toolset-7

ccmake-命令行界面,交互模式。

安装 libz,curl,openssl 和 libuuid 的 devel 版

只编译部分模块

通过 cmake 配置生成模块,cmake /mnt/hd03.d/xdu.d/aws/source/aws-sdk-cpp -DBUILD_ONLY="s3"

  • BUILD_SHARED_LIBS,是否生成动态库。ON 动态库,OFF 静态库
  • CMAKE_BUILD_TYPE,生成类型,Relaese,Debug -DCMAKE_BUILD_TYPE=Release
cmake /mnt/hd03.d/xdu.d/aws/source/aws-sdk-cpp  -DBUILD_ONLY="core;s3;transfer"  -DBUILD_SHARED_LIBS="OFF" -DCMAKE_BUILD_TYPE=Release

动态库

cmake /mnt/hd03.d/xdu.d/aws/source/aws-sdk-cpp  -DBUILD_ONLY="core;s3;transfer"  -DBUILD_SHARED_LIBS="ON" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_C_FLAGS=-fPIC

链接

动态链接需要定义 USE_IMPORT_EXPORT

If you dynamically link to the SDK you will need to define the USE_IMPORT_EXPORT symbol for all build targets using the SDK

GCC: what are the --start-group and --end-group command line options?

It is for resolving circular dependences between several libraries

Impossible to link static libraries with circular dependencies

在 aws 的静态库中,有循环引用问题。

In a project we have multiple static libraries (build using cdt), two of which have circular dependencies to each other, that link together into an executable (also built using cdt).

With the "GCC C Linker" selected it's impossible to build that file. The linker complains about missing references when the static libraries are simply added in the "C/(C++ Build -> Settings -> GCC C Linker -> Libraries" tab, because references of the library that appears last on the generated command line has undefined references.

The solution would be to add "--start-group -lX -lY --end-group" to the linker command line after the list of the object files. But that's simply not possible. The additional linker options (both the "Linker flags" and "Other options (-Xlinker [option])" fields) that can be specified on the "GCC C Linker -> Miscellaneous" tab are added to the command line before the object, which has essentially no effect.

N.B. In our project I have installed a (dirty) workaround: I have added the linker options (-Wl,--start-group,-lX,-lY,--end-group) to the "Other objects" field (and added a dummy make rule in makefile.init so make won't complain about it) to force CDT to put it after the list of objects. It works but is really ugly.

使用 GCC 可将链接参数传递给链接器使用 -Wl,--start-group-Wl,--end-group

-Wl,--start-group  -laws-cpp-sdk-core -laws-cpp-sdk-s3 -laws-cpp-sdk-transfer	-Wl,--end-group
//linux makefile
CC = g++
CCFLAGS = -DNDEBUG  -std=c++11 -O0
INCLUDE := -I../
LDFLAGS := -L../aws/bin/linux/static -Wl,-rpath ./ -Wl,-rpath ../aws/bin/linux/dynamic
LIBS  := -lcurl -pthread -lcrypto -Wl,--start-group -laws-cpp-sdk-core -laws-cpp-sdk-s3 -laws-cpp-sdk-transfer -Wl,--end-group

make: main
        rm *.o
        @echo success
main:
        $(CC) $(CCFLAGS) $(INCLUDE) -c -o main.o main.cpp
        $(CC) $(CCFLAGS) $(INCLUDE) $(LDFLAGS) -o transfer main.o $(LIBS)

Mac 下不需要 -pthread -lcrypto

查看动态库依赖

readelf -d libbar.so

$ readelf -d ../aws/bin/linux/dynamic/libaws-cpp-sdk-transfer.so

Dynamic section at offset 0x29c38 contains 36 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libaws-cpp-sdk-s3.so]
 0x0000000000000001 (NEEDED)             Shared library: [libaws-cpp-sdk-core.so]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libcurl.so.4]
 0x0000000000000001 (NEEDED)             Shared library: [libssl.so.10]
 0x0000000000000001 (NEEDED)             Shared library: [libcrypto.so.10]
 0x0000000000000001 (NEEDED)             Shared library: [libz.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000e (SONAME)             Library soname: [libaws-cpp-sdk-transfer.so]

从静态库合并为动态库

gcc -shared -o libmerge.so -Wl,--whole-archive libaws-cpp-sdk-core.a libaws-cpp-sdk-s3.a libaws-cpp-sdk-transfer.a -Wl,--no-whole-archive

windows 编译链接 aws lib

使用 cmake 生成工程,到生成库目录下,此处打开PowerShell 然后运行指令

& "E:\Software Install\cmake-3.11.0-rc3-win64-x64\cmake-3.11.0-rc3-win64-x64\bin\cmake" E:\libs\aws-sdk-cpp-1.4.11 -DBUILD_ONLY="transfer" -DBUILD_SHARED_LIBS=ON -DSTATIC_LINKING=1 -DTARGET=WINDOWS -G "Visual Studio 15 2017"

windows 静态链接 aws static lib 需要依赖于几个系统静态库

 winhttp.lib
 wininet.lib
 bcrypt.lib
 userenv.lib
 Version.lib

静态链接出现 GetObjectA/GetObjectW 错误,由于 wingdi.h 中定义了

//wingdi.h
#ifdef UNICODE
#define GetObject  GetObjectW
#else
#define GetObject  GetObjectA
#endif // !UNICODE

S3Client.h 被包含之前取消 GetObject 定义

//on windows visual studio
//undef GetObject to avoid marco conflict
#if defined _WIN32 && defined GetObject
#undef GetObject
#endif
  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1063 引用 • 3453 回帖 • 201 关注
  • AWS
    11 引用 • 28 回帖
  • C++

    C++ 是在 C 语言的基础上开发的一种通用编程语言,应用广泛。C++ 支持多种编程范式,面向对象编程、泛型编程和过程化编程。

    107 引用 • 153 回帖 • 3 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...