Как правильно включить libssh в C - PullRequest
0 голосов
/ 19 января 2019

Я получаю сообщение об ошибке каждый раз, когда пытаюсь скомпилировать свой код с помощью gcc в Ubuntu.

Я установил libssh-dev, набрав:

sudo apt-get install libssh-dev

, и он установился просто отлично (Нет сообщений об ошибках)

Код, который я пытаюсь скомпилировать:

#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);
}

Когда я пытаюсь скомпилировать код, сообщение об ошибке гласит:

user@neodym:~/Desktop/projects/ssh$ gcc -lssh ssh_client.c 
/tmp/ccGihId0.o: In function `main':
ssh_client.c:(.text+0x2a): undefined reference to `ssh_new'
ssh_client.c:(.text+0x57): undefined reference to `ssh_options_set'
ssh_client.c:(.text+0x6c): undefined reference to `ssh_options_set'
ssh_client.c:(.text+0x84): undefined reference to `ssh_options_set'
ssh_client.c:(.text+0x90): undefined reference to `ssh_connect'
ssh_client.c:(.text+0xa5): undefined reference to `ssh_get_error'
ssh_client.c:(.text+0xe2): undefined reference to `ssh_userauth_password'
ssh_client.c:(.text+0xee): undefined reference to `ssh_disconnect'
ssh_client.c:(.text+0xfa): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status

Iвсе готово, гуглил, но пока ничего не получалось.

Заголовочные файлы libssh устанавливаются в / usr / include / libssh / , поэтому gcc должен их найти.

Можете ли вы помочь мне исправить это?

1 Ответ

0 голосов
/ 19 января 2019

Попробуйте скомпилировать с:

gcc -c ssh_client.c

, а затем связать с ssh библиотекой с:

gcc -o ssh_client ssh_client.o -lssh

Или в один шаг:

gcc -o ssh_client ssh_client.c -lssh
...