簡體   English   中英

如何將.so文件鏈接到cpp程序

[英]How to link .so file to a cpp program

我的本地機器上有libssh2.so.1.0.1(.so)二進制文件,我的機器上沒有任何頭文件。

這是我一直嘗試通過ssh協議連接到我的服務器的基本ssh程序。 參考: 如何使用c ++建立簡單的ssh連接

現在我無法將庫(libssh2.so.1.0.1)鏈接到下面的示例程序。

以下是我編寫的示例程序,后面跟着錯誤。

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

我用下面的命令編譯了上面的文件

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

但是我收到以下錯誤

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

任何建議/幫助都會很有用

提前致謝 ...

您需要將libssh2頭文件包含在調用ssh API的編譯單元中。 如果沒有這個,你不能指望編譯器解決ssh_session的問題。 如果已正確安裝庫,則應該可以訪問頭文件以調用它。

#include <libssh2.h>

編輯:老實說,您在示例中使用的API屬於原始libssh庫我在您的示例中看不到需要與libssh2鏈接的任何內容。

#include <libssh/libssh.h>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM