You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04交叉编译libssh2 for ARMv7遇问题求助

交叉编译libssh2到ARMv7的编译与链接问题

环境信息

  • 宿主机:Ubuntu 22.04 x64
  • 目标架构:ARMv7
  • 交叉编译器:gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf

问题1:使用g++配置成功但链接失败

执行以下configure命令可完成配置:

CXX=/path/to/arm-linux-gnueabihf-g++ \
--host=arm-linux-gnueabihf \
--with-libssl-prefix=[DIR] \
CXXFLAGS=-m32

但链接生成的libssh2.so时,链接器报错:libssh2.so. File format not recognized。注:可正常链接他人使用同编译器编译的旧版libssh2。

问题2:使用gcc配置直接失败

执行以下configure命令:

CC=/path/to/arm-linux-gnueabihf-gcc \
--host=arm-linux-gnueabihf \
--with-libssl-prefix=[DIR] \
CFLAGS=-m32

报错信息为C compiler cannot create executables,关键日志片段:

configure:4524: /path/to/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc -m32   conftest.c  >&5
arm-linux-gnueabihf-gcc: error: unrecognized command line option '-m32'
configure:4528: $? = 1
configure:4568: result: no

问题原因

-m32是x86架构编译器的选项,用于生成32位x86代码,ARM交叉编译器不支持该选项:

  • 用gcc时,configure直接检测到编译器无法识别-m32,因此报错无法创建可执行文件。
  • 用g++时,configure可能跳过了部分严格检测,但编译器因-m32生成了错误格式的目标文件,导致最终链接时提示格式不识别。

解决方案

  1. 移除-m32编译选项:ARMv7本身是32位架构,交叉编译器默认生成ARMv7的32位代码,无需额外指定-m32。
  2. 正确的configure命令(gcc版本):
CC=/path/to/arm-linux-gnueabihf-gcc \
--host=arm-linux-gnueabihf \
--with-libssl-prefix=[DIR]
  1. 正确的configure命令(g++版本):
CXX=/path/to/arm-linux-gnueabihf-g++ \
--host=arm-linux-gnueabihf \
--with-libssl-prefix=[DIR]
  1. 验证库格式:编译完成后,执行file libssh2.so检查输出,确认是ARM架构的32位库,示例输出:
ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=..., not stripped

内容的提问来源于stack exchange,提问作者Nikzeno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 18:44:53