I'm hoping someone might have some ideas for me before I pull the rest of my hair out. I'm working with a Raspberry Pi 3 and libssh-dev and having problems getting the code to compile. Any thoughts?
I installed libssh-dev from the default repository in Raspbian Jessie and have spent the entire day trying to get the libraries included when compiling. Here is my test code, with the user and host set appropriately when I compile.
#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;
}
I'm using the following command to compile the code.
gcc -DLIBSSH_STATIC test.c -o test
Here are the error messages I'm getting. My assumption is that the libraries aren't including correctly?
/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
You're right that the libraries aren't included correctly.
#include <libssh/libssh.h>
just tells the compiler which functions libssh provides and how to call them, but you also need to tell the linker where to find the binary library.
To link against the static libssh binary, add the path to the static archive (probably /usr/lib/arm-linux-gnueabihf/libssh.a
) to the compiler command line.