Home > Net >  ssh_connect: Library not initialized (LibSSH)
ssh_connect: Library not initialized (LibSSH)

Time:09-27

I have the following piece of code which I am trying to build statically, so I end up with a single executable.

#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);
}

I have installed the following libraries using VCPKG

libssh:x86-windows-static
zlib:x86-windows-static
openssh:x86-windows-static

I have manually linked the following include path, in the C/C section of project properties on the General tab under Additional include directories

C:\dev\vcpkg\installed\x64-windows-static\include

I have also under in the Linker section of project properties, also on its General tab, added an entry for Additional library directories

C:\dev\vcpkg\installed\x64-windows-static\lib

The additional libraries are linked in the code, using the following four lines of code:

#pragma comment(lib, "mbedcrypto.lib")
#pragma comment(lib, "pthreadVSE3.lib")
#pragma comment(lib, "ssh.lib")
#pragma comment(lib, "Ws2_32.lib")

I have also set the C/C , Code Generation, Runtime option to Multi-Threaded (/MT)

When I run the program it compiles fine, creating a single executable. However, when I run the program, I get an error stating "ssh_connect: Library not initialized" enter image description here

enter image description here

This is day three of trying to get this to work, with no previous knowledge of how to compile applications. Any help greatly appreciated :)

CodePudding user response:

3 days spent on guessing instead of reading the manual - it's unbelievable. Almost on the top:

  • If libssh is statically linked, threading must be initialized by calling ssh_init() before using any of libssh provided functions. This initialization must be done outside of any threading context. Don't forget to call ssh_finalize() to avoid memory leak

By the way, any examples of libbssh usage have calls to ssh_init() and ssh_finalize(). You can look at the unit tests.

  • Related