尽管遵循此处和其他地方的说明,但无法在 Raspbian 中使用 libssh 编译代码

Can't get code to compile using libssh in Raspbian in spite of following instructions here and elsewhere

我希望在我拔掉剩下的头发之前有人能给我一些想法。我正在使用 Raspberry Pi 3 和 libssh-dev,但在编译代码时遇到问题。有什么想法吗?

我从 Raspbian Jessie 的默认存储库安装了 libssh-dev,并花了一整天的时间试图在编译时包含这些库。这是我的测试代码,编译时正确设置了用户和主机。

#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
  ssh_session my_ssh_session;
  int verbosity = SSH_LOG_PROTOCOL;
  int port = 22;

  my_ssh_session = ssh_new();
  if (my_ssh_session == NULL)
    exit (-1);

  ssh_options_set (my_ssh_session, SSH_OPTIONS_HOST, "localhost");
  ssh_options_set (my_ssh_session, SSH_OPTIONS_USER, "username");
  ssh_options_set (my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
  ssh_options_set (my_ssh_session, SSH_OPTIONS_PORT, &port);

  // other code here

  ssh_free(my_ssh_session);

  return 0;
}

我正在使用以下命令编译代码。

gcc -DLIBSSH_STATIC test.c -o test

这是我收到的错误消息。我的假设是库没有正确包含?

/tmp/ccVIYUs5.o: In function `main':
printschedules.c:(.text+0x1c): undefined reference to `ssh_new'
printschedules.c:(.text+0x44): undefined reference to `ssh_options_set'
printschedules.c:(.text+0x54): undefined reference to `ssh_options_set'
printschedules.c:(.text+0x68): undefined reference to `ssh_options_set'
printschedules.c:(.text+0x7c): undefined reference to `ssh_options_set'
printschedules.c:(.text+0x84): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status

您说得对,库没有正确包含。

#include <libssh/libssh.h> 只是告诉编译器 libssh 提供了哪些函数以及如何调用它们,但您还需要告诉 linker 在哪里可以找到二进制库。

要link针对静态libssh二进制文件,将静态存档的路径(可能/usr/lib/arm-linux-gnueabihf/libssh.a)添加到编译器命令行。