Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libssh2 C compilation

Its my first time working on a project in C. im just trying to make a SSH client.

my code copied from libssh2 site

http://www.libssh2.org/examples/ssh2.html

But when i run this i get the following errors ?

main.o: In function `main':
main.c:(.text+0xbe): undefined reference to `libssh2_init'
main.c:(.text+0x1a7): undefined reference to `libssh2_session_init_ex'
main.c:(.text+0x1bf): undefined reference to `libssh2_session_startup'
main.c:(.text+0x209): undefined reference to `libssh2_hostkey_hash'
main.c:(.text+0x282): undefined reference to `libssh2_userauth_list'
main.c:(.text+0x3e3): undefined reference to `libssh2_userauth_password_ex'
main.c:(.text+0x443): undefined reference to `libssh2_userauth_keyboard_interactive_ex'
main.c:(.text+0x4b9): undefined reference to `libssh2_userauth_publickey_fromfile_ex'
main.c:(.text+0x529): undefined reference to `libssh2_channel_open_ex'
main.c:(.text+0x58d): undefined reference to `libssh2_channel_setenv_ex'
main.c:(.text+0x5d9): undefined reference to `libssh2_channel_request_pty_ex'
main.c:(.text+0x633): undefined reference to `libssh2_channel_process_startup'
main.c:(.text+0x674): undefined reference to `libssh2_channel_free'
main.c:(.text+0x6a0): undefined reference to `libssh2_session_disconnect_ex'
main.c:(.text+0x6ac): undefined reference to `libssh2_session_free'
main.c:(.text+0x6c9): undefined reference to `libssh2_exit'
collect2: ld returned 1 exit status

I know there is some issue with library linking. Im using Netbeans as my IDE and am relatively new to this. Can anyone please guide me. I have searched a lot on the Internet, But mostly replies are related to code compiled on the command using gcc. If someone can help in Netbeans.

like image 578
Hani Q Avatar asked Jul 20 '11 12:07

Hani Q


2 Answers

Got it , in Netbeans goto File -> Project Properties -> Linker -> Additional Commands i wrote -lssh2

like image 196
Hani Q Avatar answered Sep 24 '22 10:09

Hani Q


To compile that code you need to install libssh2 in your system. Those undefined references come from the functions included in the header files (and used in the code) that need to be linked to the library where they are implemented.

#include <libssh2.h>
#include <libssh2_sftp.h>

After installing libssh2 you must add the flag -lssh2 to the compiler. I don't know about Netbeans so I can't help you with that. Browse through the project options and look for something about the compiler parameters/flags.

like image 22
nmat Avatar answered Sep 22 '22 10:09

nmat