为 armhf 编译 Crypto++ 以进行交叉编译

Compiling Crypto++ for armhf for cross compiling

我想交叉编译库 crypto++ 以便在 beaglebone 运行ning Debian 上部署。我的主机 运行s Ubuntu 64 位配置的 14.04 LTS。

当我从eclipse调用make命令时遇到以下问题

arm-linux-gnueabihf-g++-4.8 -L/usr/include/cryptopp -o "GCMwithAES"  ./main.o   -lcryptopp
/usr/lib/../lib/libcryptopp.so: file not recognized: File format not recognized

我的猜测是,由于编译器是为 armhf 配置的,它无法识别为 amd64 编译的库。

我已经成功地从主机 PC 到目标设备交叉编译和 运行 标准(即没有外部库)程序。

我尝试过的解决方案

如何在基于 x64 的 PC 上安装 armhf 架构的 libcryptopp 库以便交叉编译?或者有没有其他方法可以解决这个问题。

编辑

按照下面答案中的建议,我尝试了建议的方法。我稍微修改了脚本 setenv-embed.sh,因为我有 gcc-4.8 而不是 gcc-4.7。 运行脚本的结果是

CPP: /usr/bin/arm-linux-gnueabihf-cpp 
CXX: /usr/bin/arm-linux-gnueabihf-g++
AR: /usr/bin/arm-linux-gnueabihf-ar
LD: /usr/bin/arm-linux-gnueabihf-ld
RANLIB: /usr/bin/arm-linux-gnueabihf-gcc-ranlib-4.8

ARM_EMBEDDED_TOOLCHAIN: /usr/bin
ARM_EMBEDDED_CXX_HEADERS: /usr/arm-linux-gnueabihf/include/c++/4.8.2
ARM_EMBEDDED_FLAGS: -march=armv7-a mfloat-abi=hard -mfpu=neon -I/usr/arm-linux-gnueabihf/include/c++/4.8.2 -I/usr/arm-linux-gnueabihf/include/c++/4.8.2/arm-linux-gnueabihf 
ARM_EMBEDDED_SYSROOT: /usr/arm-linux-gnueabihf

我用make命令建库,运行出现如下错误

/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/‌​ld: cannot find /usr/arm-linux-gnueabihf/lib/libc.so.6 inside /usr/arm-linux-gnueabihf
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/‌​ld: cannot find /usr/arm-linux-gnueabihf/lib/libc_nonshared.a inside /usr/arm-linux-gnueabihf        
/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/‌​ld: cannot find /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3 inside /usr/arm-linux-gnueabihf

但是当我打开位置 /usr/arm-linux-gnueabihf/lib 时,我可以找到上面提到的所有三个错误文件,即 libc.so.6libc_nonshared.ald-linux-armhf.so.3

根据@jww 的建议,我将此问题转移到一个新问题,因为我遇到了 linking 问题。我的结果留在这里是为了完整性。

How do I install the libcryptopp libraries of the armhf architecture on my x64 based PC so I can cross compile? or is there any other way to resolve this issue.

在 Crypto++ wiki 上查看 ARM Embedded (Command Line)

注意:该 wiki 页面有点过时了。您现在可以使用 GNUmakefile-cross. I have not updated the page to reflect recent changes like GNUmakefile-cross.

GNUmakefile-cross 是为 Android、iOS、Windows Phone、ARM Embedded 和裸机(我怀疑后来有人会这样做,但我将其作为平台进行了测试)。您仍然需要 运行 setenv-embedded.sh 脚本。

要从 GitHub 获取最新资源:

git clone https://github.com/weidai11/cryptopp.git cryptopp-armhf

GitHub 来源目前非常活跃。我们正在准备 Crypto++ 5.6.3 版本。 5.6.3 将包括 GNUmakefile-cross.


完整的说明如下所示(假设您已安装工具):

git clone https://github.com/weidai11/cryptopp.git cryptopp-armhf
cd cryptopp-armhf

# Note the leading dot!!!
. ./setenv-embedded.sh
# The command above must execute successfully
# It cannot display a message like "**CXX is not valid**"

# Build it
make -f GNUmakefile-cross static dynamic cryptest.exe

# Check it
$ find . -name cryptest.exe
./cryptest.exe
$ /usr/bin/arm-linux-gnueabi-readelf -h ./cryptest.exe | grep -i 'class\|machine' 
  Class:                             ELF32
  Machine: 

因为 GitHub 来源目前非常活跃,我已经将 Crypto++-Mobile.zipSetenv-embedded.sh.zip 中的所有其他文件添加到官方 Crypto++来源。你只需要从 Setenv-embedded.sh.zip.

中得到 setenv-embedded.sh

除了,我还想补充一些注意事项。 (这些说明与 2015 年 11 月 20 日发布的 5.6.3 版相关。)

可能需要编辑 config.h 文件以更改某些选项。参见 Config.h on the Crypto++ wiki。特别是:

  • CRYPTOPP_NO_UNALIGNED_DATA_ACCESS 可能需要定义,以便代码在无法执行未对齐数据的系统上正常运行 read/writes(例如 ARM)。
  • CRYPTOPP_INIT_PRIORITYCRYPTOPP_USER_PRIORITY 可能需要定义。有关详细信息,请参阅 Static Initialization Order Fiasco - Crypto++ Wiki
  • CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 可能需要定义或取消定义,具体取决于使用它的项目是否使用较旧的 API 功能。

构建库后,非常值得 运行 在目标系统上测试程序 cryptest.exe v,以检查是否为该系统构建了库。例如,通过这样做,我发现该库无法在基于 ARM 的 BeagleBone Black 上正常工作,除非我在 config.h 中定义 CRYPTOPP_NO_UNALIGNED_DATA_ACCESS(它在测试步骤 [=18= 中无限期冻结) ]).