libssh交叉编译

libssh下载

libssh介绍网址:https://www.libssh.org

官方全版本下载:Index of /files

git clone:git clone https://git.libssh.org/projects/libssh.git libssh

以下载的libssh-0.10.6为例:

libssh-0.10.6.tar.xz

#解压并进入libssh-0.10.6目录
tar xf libssh-0.10.6.tar.xz
cd libssh-0.10.6

编译说明:

编译说明在libssh-0.10.6源码文件夹下的INSTALL中,里面的内容如下:

In order to build libssh, you need to install several components:

optional:

Note that these version numbers are version we know works correctly. If you
build and run libssh successfully with an older version, please let us know.

For Windows use vcpkg:

https://github.com/Microsoft/vcpkg

which you can use to install openssl and zlib. libssh itself is also part of
vcpkg!

要构建 libssh,您需要安装以下几个组件:

选项:

请注意,这里列出的版本号是我们已知能够正常工作的版本。
如果您使用更旧的版本也能成功构建并运行 libssh,请告知我们。

在 Windows 平台上,请使用 vcpkg

https://github.com/Microsoft/vcpkg

通过 vcpkg 可以方便地安装 OpenSSL 和 zlib。此外,libssh 本身也已包含在 vcpkg 中

Building-构建

First, you need to configure the compilation, using CMake. Go inside the
build dir. Create it if it doesn’t exist.

GNU/Lnux, MacOS X, MSYS/MinGW:

cmake -DUNIT_TESTING=ON -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug ..
make

On Windows you should choose a makefile generator with -G or use.

cmake-gui.exe ..

To enable additional client tests against a local OpenSSH server, add the
compile option -DCLIENT_TESTING=ON. These tests require an OpenSSH
server package and some wrapper libraries (see optional requirements) to
be installed.

If you’re interested in server testing, then a OpenSSH client should be
installed on the system and if possible also dropbear. Once that is done
enable server support with -DWITH_SERVER=ON and enable testing of it with
-DSERVER_TESTING=ON.

首先,您需要使用 CMake 来配置编译过程。进入 build 目录;如果该目录不存在,请先创建它。

cmake -DUNIT_TESTING=ON -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug ..
make

在 Windows 上,您应通过 -G 选项指定一个 Makefile 生成器,或者使用其他构建系统(如 Visual Studio 项目生成器)。

cmake-gui.exe ..

若要启用针对本地 OpenSSH 服务器的额外客户端测试,请添加编译选项 -DCLIENT_TESTING=ON
这些测试需要系统中已安装 OpenSSH 服务器软件包 以及一些 封装库(wrapper libraries)(详见可选依赖项)。

如果您希望进行 服务器端测试,则需在系统中安装 OpenSSH 客户端,并且(如果可能)也安装 Dropbear
完成上述安装后,可通过以下 CMake 选项启用相关功能:

Testing build-构建测试

make test
CMake standard options-C make标准选项

Here is a list of the most interesting options provided out of the box by
CMake.

  • CMAKE_BUILD_TYPE: The type of build (can be Debug Release MinSizeRel
    RelWithDebInfo)
  • CMAKE_INSTALL_PREFIX: The prefix to use when running make install (Default
    to /usr/local on GNU/Linux and MacOS X)
  • CMAKE_C_COMPILER: The path to the C compiler
  • CMAKE_CXX_COMPILER: The path to the C++ compiler

以下是 CMake 自带的一些常用且重要的配置选项:

  • CMAKE_BUILD_TYPE

    指定构建类型,可选值包括:

    • Debug(带调试信息,无优化)
    • Release(全优化,无调试信息)
    • MinSizeRel(优化以减小体积)
    • RelWithDebInfo(优化并包含调试信息)
  • CMAKE_INSTALL_PREFIX
    指定执行 make install(或 cmake --install)时的安装前缀。
    在 GNU/Linux 和 macOS 上默认为 /usr/local

  • CMAKE_C_COMPILER
    指定 C 编译器的路径(如 gccclang 等)。

  • CMAKE_CXX_COMPILER
    指定 C++ 编译器的路径(如 g++clang++ 等)。

CMake options defined for libssh-libssh-自定义的 CMake 选项

Options are defined in the following files:

  • DefineOptions.cmake

They can be changed with the -D option:

cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug -DWITH_ZLIB=OFF ..

libssh 项目在以下文件中定义了其特有的构建选项:

DefineOptions.cmake

这些选项可通过 -D 参数在命令行中设置,例如:

cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug -DWITH_ZLIB=OFF ..
Browsing/editing CMake options-浏览和编辑 CMake 选项

In addition to passing options on the command line, you can browse and edit
CMake options using cmakesetup (Windows), cmake-gui or ccmake (GNU/Linux
and MacOS X).

  • Go to the build dir
  • On Windows: run cmakesetup
  • On GNU/Linux and MacOS X: run ccmake ..

除了在命令行传递选项外,您还可以使用图形化或交互式工具来浏览和修改 CMake 配置:

  • Windows:运行 cmakesetup(旧版)或更推荐使用 cmake-gui
  • GNU/Linux 和 macOS:运行 ccmake ..(基于终端的交互界面)或 cmake-gui

操作步骤:

  1. 进入构建目录(build/);
  2. 根据平台运行相应工具:
    • Windows: cmake-gui ..
    • Linux/macOS: ccmake ..cmake-gui ..
Useful Windows options-有效的Windows配置选项:

If you have installed OpenSSL or ZLIB in non standard directories, maybe you
want to set:

OPENSSL_ROOT_DIR

and

ZLIB_ROOT_DIR

如果您将 OpenSSLZLIB 安装在非标准目录(即不在系统 PATH 或默认搜索路径中),您可能需要手动指定它们的安装根目录,以便 CMake 能正确找到相关头文件和库。

请设置以下变量:

  • OPENSSL_ROOT_DIR:指向 OpenSSL 的安装根目录(该目录下通常包含 include/lib/ 子目录)。
  • ZLIB_ROOT_DIR:指向 Zlib 的安装根目录(同样应包含 include/lib/)。

Installing-安装

If you want to install libssh after compilation run:

make install

如果要在编译完成后安装 libssh,请运行:

make install

Running-运行

The libssh binary can be found in the build/src directory.
You can use build/examples/samplessh which is a sample client to
test libssh on UNIX.

编译生成的 libssh 二进制文件位于 build/src/ 目录下。

在 UNIX 系统上,您可以使用示例客户端程序 build/examples/samplessh 来测试 libssh 的功能。

About this document-关于本文档

This document is written using Markdown syntax, making it possible to
provide usable information in both plain text and HTML format. Whenever
modifying this document please use Markdown syntax.

本文档采用 Markdown 语法 编写,因此既能以纯文本形式清晰阅读,也可轻松转换为 HTML 等格式。

📝 请注意
今后修改本文档时,请务必继续使用 Markdown 语法

更多关于 Markdown 的信息,请参见:
https://www.daringfireball.net/projects/markdown

DefineOptions.cmake文档说明

DefineOptions.cmake文档内容如下:

option(WITH_GSSAPI "Build with GSSAPI support" ON)
option(WITH_ZLIB "Build with ZLIB support" ON)
option(WITH_SFTP "Build with SFTP support" ON)
option(WITH_SERVER "Build with SSH server support" ON)
option(WITH_DEBUG_CRYPTO "Build with crypto debug output" OFF)
option(WITH_DEBUG_PACKET "Build with packet debug output" OFF)
option(WITH_DEBUG_CALLTRACE "Build with calltrace debug output" ON)
option(WITH_DSA "Build with DSA" OFF)
option(WITH_GCRYPT "Compile against libgcrypt" OFF)
option(WITH_MBEDTLS "Compile against libmbedtls" OFF)
option(WITH_BLOWFISH_CIPHER "Compile with blowfish support" OFF)
option(WITH_PCAP "Compile with Pcap generation support" ON)
option(WITH_INTERNAL_DOC "Compile doxygen internal documentation" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(WITH_PKCS11_URI "Build with PKCS#11 URI support" OFF)
option(UNIT_TESTING "Build with unit tests" OFF)
option(CLIENT_TESTING "Build with client tests; requires openssh" OFF)
option(SERVER_TESTING "Build with server tests; requires openssh and dropbear" OFF)
option(WITH_BENCHMARKS "Build benchmarks tools; enables unit testing and client tests" OFF)
option(WITH_EXAMPLES "Build examples" ON)
option(WITH_NACL "Build with libnacl (curve25519)" ON)
option(WITH_SYMBOL_VERSIONING "Build with symbol versioning" ON)
option(WITH_ABI_BREAK "Allow ABI break" OFF)
option(WITH_GEX "Enable DH Group exchange mechanisms" ON)
option(WITH_INSECURE_NONE "Enable insecure none cipher and MAC algorithms (not suitable for production!)" OFF)
option(FUZZ_TESTING "Build with fuzzer for the server and client (automatically enables none cipher!)" OFF)
option(PICKY_DEVELOPER "Build with picky developer flags" OFF)

if (WITH_ZLIB)
    set(WITH_LIBZ ON)
else (WITH_ZLIB)
    set(WITH_LIBZ OFF)
endif (WITH_ZLIB)

if (WITH_BENCHMARKS)
  set(UNIT_TESTING ON)
  set(CLIENT_TESTING ON)
endif()

if (UNIT_TESTING OR CLIENT_TESTING OR SERVER_TESTING)
  set(BUILD_STATIC_LIB ON)
endif()

if (WITH_NACL)
  set(WITH_NACL ON)
endif (WITH_NACL)

if (WITH_ABI_BREAK)
  set(WITH_SYMBOL_VERSIONING ON)
endif (WITH_ABI_BREAK)

if (NOT GLOBAL_BIND_CONFIG)
  set(GLOBAL_BIND_CONFIG "/etc/ssh/libssh_server_config")
endif (NOT GLOBAL_BIND_CONFIG)

if (NOT GLOBAL_CLIENT_CONFIG)
  set(GLOBAL_CLIENT_CONFIG "/etc/ssh/ssh_config")
endif (NOT GLOBAL_CLIENT_CONFIG)

if (FUZZ_TESTING)
  set(WITH_INSECURE_NONE ON)
endif (FUZZ_TESTING)

中文说明:

功能支持选项:
WITH_GSSAPI(默认:ON)
启用 GSSAPI(Generic Security Services API)认证支持,常用于 Kerberos 等企业级身份验证。
WITH_ZLIB(默认:ON)
启用 ZLIB 压缩支持,允许 SSH 会话中使用压缩传输数据。
WITH_SFTP(默认:ON)
启用 SFTP(SSH 文件传输协议)子系统支持。
WITH_SERVER(默认:ON)
启用 SSH 服务器功能(若仅需客户端可关闭)。
WITH_DSA(默认:OFF)
启用已不推荐使用的 DSA 密钥算法(出于安全考虑,默认禁用)。
WITH_BLOWFISH_CIPHER(默认:OFF)
启用 Blowfish 加密算法(现代系统通常不推荐使用)。
WITH_GEX(默认:ON)
启用 Diffie-Hellman Group Exchange(DH-GEX)密钥交换机制,提升前向安全性。
WITH_INSECURE_NONE(默认:OFF)
启用不安全的 "none" 加密与 MAC 算法(仅用于测试,切勿用于生产环境!)
WITH_PKCS11_URI(默认:OFF)
支持通过 PKCS#11 URI 引用硬件安全模块(HSM)或智能卡中的密钥。
WITH_NACL(默认:ON)
启用 libnacl 库支持,提供 Curve25519 密钥交换(高性能、现代加密)。

加密后端选项(互斥)
通常只启用其中一个,或使用 OpenSSL(默认)。
WITH_GCRYPT(默认:OFF)
使用 libgcrypt 作为底层加密库(如 GnuPG 所用)。
WITH_MBEDTLS(默认:OFF)
使用 mbed TLS(轻量级嵌入式 TLS 库)作为加密后端。
若以上均未启用,libssh 默认使用 OpenSSL。

调试与日志选项
WITH_DEBUG_CRYPTO(默认:OFF)
输出加密相关调试信息(可能泄露敏感数据,慎用)。
WITH_DEBUG_PACKET(默认:OFF)
打印原始 SSH 数据包内容(用于协议分析)。
WITH_DEBUG_CALLTRACE(默认:ON)
启用函数调用追踪日志,便于调试执行流程。
WITH_PCAP(默认:ON)
支持生成 pcap 格式的网络包捕获文件(可用于 Wireshark 分析)。

构建与开发选项
BUILD_SHARED_LIBS(默认:ON)
构建共享库(.so / .dll);若设为 OFF 则仅构建静态库。
WITH_SYMBOL_VERSIONING(默认:ON)
启用符号版本控制(Symbol Versioning),提升共享库的 ABI 兼容性(Linux 特有)。
WITH_ABI_BREAK(默认:OFF)
允许破坏 ABI 兼容性(仅限开发阶段,正式发布应保持 ABI 稳定)。
WITH_INTERNAL_DOC(默认:OFF)
使用 Doxygen 生成内部开发文档。
WITH_EXAMPLES(默认:ON)
编译示例程序(如 samplessh)。

测试与基准
UNIT_TESTING(默认:OFF)
启用单元测试。
CLIENT_TESTING(默认:OFF)
启用客户端集成测试(需系统安装 OpenSSH 服务器)。
SERVER_TESTING(默认:OFF)
启用服务器端集成测试(需 OpenSSH 客户端和 Dropbear)。
WITH_BENCHMARKS(默认:OFF)
构建性能基准测试工具(自动启用单元测试和客户端测试)。
FUZZ_TESTING(默认:OFF)
启用模糊测试(fuzzer)支持,会自动启用不安全的 "none" 算法,仅用于安全测试。
PICKY_DEVELOPER(默认:OFF)
启用严格的编译器警告标志(如 -Wall -Wextra -Werror),适用于开发者。

交叉编译libssh:

解压后的libssh文件夹下的文件如下:

AUTHORS  CHANGELOG  CMakeLists.txt        config.h.cmake         CONTRIBUTING.md  CPackConfig.cmake  DefineOptions.cmake  examples  INSTALL          README          README.md  tests
BSD      cmake      CompilerChecks.cmake  ConfigureChecks.cmake  COPYING          CTestConfig.cmake  doc                  include   libssh.pc.cmake  README.mbedtls  src

以龙芯交叉编译工具进行说明,Linux平台下编译,编译为静态库,工具链路径:

/opt/loongson-gnu-toolchain-13.2/bin

静态库存放路径:

/root/workspace/lib_libary

根据上述说明,编译libssh前需要建立一个build文件夹,所以编译前需要创建build文件夹并进入。

同时还需要用到openssl库和zlib库,那么同时需要用同样的编译工具链编译openssl以及zlib库。

对于openssl库的交叉编译请参照文章:linux下使用交叉编译工具链编译openssl-CSDN博客

对于zlib库的交叉编译请参照文章:linux下使用交叉编译工具链编译zlib-CSDN博客

编译完成后的openssl安装在/root/workspace/lib_libary目录下,那么执行libssh交叉编译为静态库的指令如下:

#创建build文件夹并进入
mkdir -p build && cd build

#执行cmake并传递编译选项
#CMAKE_C_COMPILER 指定 C 编译器的路径
#CMAKE_INSTALL_PREFIX 编译完成后libssh库安装路径
#CMAKE_BUILD_TYPE 编译版本为Release
#BUILD_SHARED_LIBS 是否编译动态库,默认是动态库,设置为OFF才是静态库
#WITH_SERVER 启用 SSH 服务器功能
#WITH_PCAP 取消PACP文件生成模块
#WITH_EXAMPLES 不启用函数调用追踪日志
#OPENSSL_ROOT_DIR 手动指定所依赖的openssl路径,头文件目录,链接的静态库目录以及静态库文件,如果不指定可能直接链接到了动态库上,这里需要注意
#ZLIB_ROOT 手动指定所依赖的zlib路径,头文件目录,链接的静态库目录以及静态库文件
cmake .. -DCMAKE_C_COMPILER=/opt/loongson-gnu-toolchain-13.2/bin/loongarch64-unknown-linux-gnu-gcc \
-DCMAKE_INSTALL_PREFIX=/root/workspace/lib_libary \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DWITH_SERVER=ON \
-DWITH_PCAP=OFF \
-DWITH_EXAMPLES=OFF \
-DWITH_DEBUG_CALLTRACE=OFF \
-DOPENSSL_ROOT_DIR=/root/workspace/lib_libary \
-DOPENSSL_INCLUDE_DIR=/root/workspace/lib_libary/include \
-DOPENSSL_CRYPTO_LIBRARY=/root/workspace/lib_libary/lib/libcrypto.a \
-DOPENSSL_SSL_LIBRARY=/root/workspace/lib_libary/lib/libssl.a \
-DZLIB_ROOT=/root/workspace/lib_libary \
-DZLIB_INCLUDE_DIR=/root/workspace/lib_libary/include \
-DZLIB_LIBRARY=/root/workspace/lib_libary/lib/libz

make
make install

编译完成后如下提示表示编译成功:

[ 90%] Building C object src/CMakeFiles/ssh.dir/sftp_aio.c.o
[ 92%] Building C object src/CMakeFiles/ssh.dir/sftpserver.c.o
[ 93%] Building C object src/CMakeFiles/ssh.dir/server.c.o
[ 95%] Building C object src/CMakeFiles/ssh.dir/bind.c.o
[ 96%] Building C object src/CMakeFiles/ssh.dir/bind_config.c.o
[ 98%] Building C object src/CMakeFiles/ssh.dir/dh-gex.c.o
[100%] Linking C static library libssh.a
[100%] Built target ssh

上述编译完成后执行执行make install后会将编译完成后的内容安装在/root/workspace/lib_libary目录下:

ls
include  lib

包含两个文件夹,其中静态库就在lib目录下。

注:文章所示例的编译openssl版本为openssl-3.6.0,但是libssh的版本为libssh-0.10.6,这两个版本不兼容;

如果想使用openssl-3.6.0版本,那么libssh的版本需要升级为>=libssh-0.11;

如果想使用libssh-0.10.6版本,那么open的版本需要降级,降级到>=openssl-1.1.1,小于openssl-3.0即可。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐