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

静态编译libssh程序出现ssh_connect: Library not initialized错误求助

静态编译libssh代码运行时出现"ssh_connect: Library not initialized"错误

代码示例

#define LIBSSH_STATIC 1
   
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <string.h>
   
#pragma comment(lib, "mbedcrypto.lib")
#pragma comment(lib, "pthreadVSE3.lib")
#pragma comment(lib, "ssh.lib")
#pragma comment(lib, "Ws2_32.lib")

int main()
{
    ssh_session my_ssh_session;
    int method, rc;
    int port = 22;
    const char* password;
    int verbosity = SSH_LOG_FUNCTIONS;
    //int stricthostcheck = 0;

    std::string host = "10.10.10.100";
    std::string user = "user";

    // Open session and set options
    my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
        exit(-1);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host.c_str());
    ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, user.c_str());
    ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

     // Connect to server
    rc = ssh_connect(my_ssh_session);
    if (rc != SSH_OK)
    {
        fprintf(stderr, "Error: %s\n", ssh_get_error(my_ssh_session));
        ssh_free(my_ssh_session);
        exit(-99);
    }
    
    // Authenticate ourselves
    password = "Password";
    rc = ssh_userauth_password(my_ssh_session, NULL, password);
    if (rc != SSH_AUTH_SUCCESS)
    {
        fprintf(stderr, "Error authenticating with password: %s\n",
            ssh_get_error(my_ssh_session));
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }

    ssh_disconnect(my_ssh_session);
    ssh_free(my_ssh_session);
}

环境配置

  • 通过VCPKG安装的静态库:
    libssh:x86-windows-static
    zlib:x86-windows-static
    openssh:x86-windows-static
    
  • 项目属性配置:
    • C/C++常规:额外包含目录 C:\dev\vcpkg\installed\x64-windows-static\include
    • 链接器常规:额外库目录 C:\dev\vcpkg\installed\x64-windows-static\lib
    • C/C++代码生成:运行时库设置为Multi-Threaded (/MT)

问题现象

编译成功,但运行时报错:ssh_connect: Library not initialized

解决方案

1. 手动初始化libssh库

静态编译libssh时,必须手动调用库初始化和清理函数,动态库会自动完成这一步,但静态库不会。修改代码如下:

int main()
{
    // 初始化libssh库
    int init_rc = ssh_init();
    if (init_rc != SSH_OK)
    {
        fprintf(stderr, "Failed to initialize libssh: %s\n", ssh_get_error(NULL));
        exit(-1);
    }

    // 原有的session创建、配置、连接代码...

    // 清理libssh库
    ssh_finalize();
    return 0;
}

2. 统一架构匹配

你安装的是x86-windows-static(32位)库,但配置的包含目录和库目录是x64-windows-static(64位),架构不匹配会导致潜在问题。需要二选一:

  • 将VCPKG安装的库切换为libssh:x64-windows-static
  • 将项目的平台工具集改为x86,并将包含/库目录改为C:\dev\vcpkg\installed\x86-windows-static\include和C:\dev\vcpkg\installed\x86-windows-static\lib

3. 确认链接的库文件正确性

VCPKG安装的静态libssh库文件名通常是libssh.lib而非ssh.lib,请检查链接的库名是否正确。可修改#pragma comment(lib, "ssh.lib")为#pragma comment(lib, "libssh.lib"),或者在链接器->输入->附加依赖项中添加libssh.lib。

同时确保所有依赖库都已正确链接:zlib、mbedtls、pthread等,VCPKG安装的静态库会自动处理依赖,但手动配置时需确认路径正确。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 03:31:50