How to include libssh to my project

P. Lacosta picture P. Lacosta · Aug 19, 2015 · Viewed 7.7k times · Source

I've installed libssh following the instructions and even though everything seems to be OK my compiler still returns the error "file not found" in the line "#include ". I guess it has something to do with directories or links (I have "make install" in the same folder where I downloaded it) but I don't know where should I put it so I can #include it in any project.

This is how I installed it: I downloaded it and unzip it into the folder "libssh" on my Desktop (Mac).

Then I did

cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug ..
make

and finally:

sudo make install

Then in my program I have:

#include <libssh/sftp.h>

And XCode returns: "libssh/sftp.h file not found". I tried adding the libssh folder in the Desktop to the project, but I still have similar problems.

I guess I should install it (somehow) to the /usr/include folder, so that any project can use it (like pthread or many others), but I don't know how to do this.

If I include any other file in /usr/include it works fine (like ) but when I #include it returns file not found, even though if I cd to /usr/include/libssh the file libssh.h does exist.

This is the very simple sample code:

#include <stdio.h>
#include <pthread.h> //OK
#include <libssh/libssh.h> //Not OK, file not found.

int main(int argc, const char * argv[])
{
    printf("Hello World!");
    return 0;
}

Answer

ckruczek picture ckruczek · Aug 19, 2015

In the tutorial is described how you have to link the library

You have two possibilities here:

  1. As described you have to add those two lines to your code

    #define LIBSSH_STATIC 1

    #include <libssh/libssh.h>

  2. You compile your code with the LIBSSH_STATIC flag.

    gcc -DLIBSSH_STATIC test.c -o test.o

I thought that if you have the library in /usr/include the compiler will automatically link it. For instance, the pthread.h file is included properly without doing anything.

This is a system library which gets linked automatically most of the time. libssh is not. Thats why you have to be more specific on how to compile/link it.