【发布时间】:2018-01-05 14:25:39
【问题描述】:
我的本地机器上有 libssh2.so.1.0.1(.so) 二进制文件,但我的机器上没有任何头文件。
这是我一直试图通过 ssh 协议连接到我的服务器的基本 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
任何建议/帮助都会很有用
提前致谢...
【问题讨论】:
-
尝试
-lssh2而不是libssh2。如果运行-L.,还要确保库在您的当前目录中,您还需要包含一个标头以包含库,因为如果您只是链接库并继续不在代码中声明它,它不会知道您的意思.