小编ano*_*non的帖子

如何在 C 中正确包含 libssh

每次我尝试在 Ubuntu 上使用 gcc 编译代码时都会收到错误。

我通过输入以下命令安装了 libssh-dev:

sudo apt-get install libssh-dev
Run Code Online (Sandbox Code Playgroud)

它安装得很好(没有错误消息)

我试图编译的代码是:

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

int main(void){
    int rc;
    int port = 21;
    char *pass = "password";

    ssh_session 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_PORT, &port);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username");

    rc = ssh_connect(my_ssh_session);
    if(rc != SSH_OK){
        fprintf(stderr, "Error connecting to localhost: %s\n", ssh_get_error(my_ssh_session) );
        exit(-1);
    } 

    ssh_userauth_password(my_ssh_session, NULL, pass);

    ssh_disconnect(my_ssh_session);
    ssh_free(my_ssh_session);
}

Run Code Online (Sandbox Code Playgroud)

当我尝试编译代码时,错误消息显示:

user@neodym:~/Desktop/projects/ssh$ gcc -lssh ssh_client.c 
/tmp/ccGihId0.o: …
Run Code Online (Sandbox Code Playgroud)

c gcc libssh

1
推荐指数
1
解决办法
3171
查看次数

标签 统计

c ×1

gcc ×1

libssh ×1