user3398900
user3398900

Reputation: 845

How to link .so file to a cpp program

I have libssh2.so.1.0.1(.so) binary on my local machine and I don't have any header files present on my machine.

This is the basic ssh program I have been trying to connect to my server through ssh protocol. Reference: How to establish a simple ssh connection with c++

Now I am unable to link library (libssh2.so.1.0.1) to the below sample program.

Following is the sample program I have written and followed by errors.

sshsample.cpp:

#include <stdlib.h>
#include <stdio.h> 
int main()
{
    ssh_session my_ssh_session;
    int rc;
    int port = 22;
    int verbosity = SSH_LOG_PROTOCOL;
    char *password;
    // 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, "192.168.1.6");
   ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "john");
   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(-1);
}
// Authenticate ourselves
password = "pass";
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 compiled the above file with below command

g++ -L. -llibssh2 -o main sshsample.cpp

but I get the following error

sshsample.cpp: In function 'int main()':
sshsample.cpp:8: error: 'ssh_session' was not declared in this scope
sshsample.cpp:8: error: expected `;' before 'my_ssh_session'
sshsample.cpp:11: error: 'SSH_LOG_PROTOCOL' was not declared in this scope
sshsample.cpp:14: error: 'my_ssh_session' was not declared in this scope
sshsample.cpp:14: error: 'ssh_new' was not declared in this scope

Any suggestions/help would be of great use

Thanks in advance ...

Upvotes: 1

Views: 1140

Answers (1)

Justin Randall
Justin Randall

Reputation: 2278

You need to include the libssh2 header file into your compilation units where ssh APIs are invoked. You cannot expect the compiler to resolve what an ssh_session is without this. If you have the library properly installed, you should have access to the header files to invoke it.

#include <libssh2.h>

Edit: Honestly the APIs you are using in your example belong to the original libssh library I don't see anything in your example that would need to link with libssh2.

#include <libssh/libssh.h>

Upvotes: 3

Related Questions