c++ - 如何将 .so 文件链接到 cpp 程序

标签 c++ ssh libssh2 .so

我的本​​地机器上有 libssh2.so.1.0.1(.so) 二进制文件,但我的机器上没有任何头文件。

这是我一直试图通过 ssh 协议(protocol)连接到我的服务器的基本 ssh 程序。 引用:How to establish a simple ssh connection with c++

现在我无法将库 (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>

关于c++ - 如何将 .so 文件链接到 cpp 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48115547/

相关文章:

c++ - libssh2 - 使用 SFTP 读取文件

php - 在 PHP 5.4.9 (x64) 上安装 libssh2-php

c++ - 具有重叠 I/O 的非阻塞套接字

c++ - 可修改右值和常量右值有什么区别?

ssh - 设置 SSH 连接超时

windows - 在 Windows 中通过 SSH 服务器的 Git,找不到共享库

c++ - 使用 libssh2 将字符串写入远程文件

c++ - 有没有办法检测混合类型和非类型的任意模板类?

c++ - OpenJPEG 的 opj_encode 是段错误

ssh - Ansible - 建立初始 SSH 连接