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



我希望在我拔掉其余头发之前,有人能对我有一些想法。我正在使用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提供了哪些函数以及如何调用它们,但您还需要告诉链接器在哪里可以找到二进制库。

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

相关内容

最新更新