User avatar
Zili
Posts: 7
Joined: Wed Jan 13, 2016 2:48 pm

How to use libssh2 in C

Mon Mar 18, 2019 3:32 pm

I'm trying to make a C program to connect to an ssh server. I used an
apt-get install libssh2-1
but the linker can't find the libraries!
Can someone help me ?
Thank you.

User avatar
rpdom
Posts: 17705
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to use libssh2 in C

Mon Mar 18, 2019 4:41 pm

You'll probably need libssh2-1-dev for building programs. libssh2-1 (no -dev) is used just for running them.
Unreadable squiggle

User avatar
PeterO
Posts: 5967
Joined: Sun Jul 22, 2012 4:14 pm

Re: How to use libssh2 in C

Mon Mar 18, 2019 5:00 pm

A useful command to use is "apt-cache search package-name-here" I'm not in fornt of a PI right now, but my MINT linux machine produced this output...

Code: Select all

$ apt-cache search  libssh2-1
libssh2-1 - SSH2 client-side library
libssh2-1-dbg - SSH2 client-side library (debug package)
libssh2-1-dev - SSH2 client-side library (development headers)
$
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
Zili
Posts: 7
Joined: Wed Jan 13, 2016 2:48 pm

Re: How to use libssh2 in C

Sun Mar 24, 2019 11:28 am

Thanks to rpdom and PeterO.

The problem was trivial.
simply the library is not called libssh2.lib but ssh2 !
The compilation command is:
gcc -Wall -Wno-unused-function myProgram.c -o myProgram.exe -l ssh2

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: How to use libssh2 in C

Sun Mar 24, 2019 1:15 pm

Zili wrote:
Sun Mar 24, 2019 11:28 am
The problem was trivial.
simply the library is not called libssh2.lib but ssh2 !
No, the library is called libssh2.lib
The C compiler driver fills in the full name for you.
For example, the math library is called "libm.so" but to use it in C you simply give "-lm"
The compiler added the "lib" at the front and the ".so" at the back.

CanteoCantineo
Posts: 3
Joined: Mon Mar 25, 2019 3:27 pm
Contact: Website Twitter

Re: How to use libssh2 in C

Mon Mar 25, 2019 3:33 pm

Can we import any C library using this method?

User avatar
jahboater
Posts: 6274
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: How to use libssh2 in C

Mon Mar 25, 2019 5:14 pm

CanteoCantineo wrote:
Mon Mar 25, 2019 3:33 pm
Can we import any C library using this method?
As far as I know that's how C compilers have worked since the dawn of time.

Heater
Posts: 16832
Joined: Tue Jul 17, 2012 3:02 pm

Re: How to use libssh2 in C

Mon Mar 25, 2019 5:36 pm

jahboater,
As far as I know that's how C compilers have worked since the dawn of time.
Indeed. On Unix anyway.

On other systems the C compilers would sensibly accept whatever name the library had. No "lib" prefix required. It is kind of weird is it not?

I recall being mightily puzzled by this when discovering Linux in 1997 or so.
Memory in C++ is a leaky abstraction .

User avatar
PeterO
Posts: 5967
Joined: Sun Jul 22, 2012 4:14 pm

Re: How to use libssh2 in C

Mon Mar 25, 2019 6:36 pm

libiberty is amusing :-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “C/C++”