link libssh with static library (libssh.a)
link libssh with static library (libssh.a)
I was trying to link my program with libssh static library.
Following is my simple code copied from libssh tutorial:
//sshtest.c
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>
int main()
{
ssh_session my_ssh_session;
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_free(my_ssh_session);
}
I put library file libssh.a
into the subdirectory libs/
libssh.a
libs/
Then compile it with command gcc sshtest.c -Llibs -lssh -o sshtest
gcc sshtest.c -Llibs -lssh -o sshtest
The output is bunch of undefined reference errors like:
libs/libssh.a(wrapper.c.o): In function `crypto_free':
/home/gg/libssh/src/wrapper.c:156: undefined reference to `BN_clear_free'
/home/gg/libssh/src/wrapper.c:157: undefined reference to `BN_clear_free'
libs/libssh.a(libcrypto.c.o): In function `ssh_reseed':
/home/gg/libssh/src/libcrypto.c:77: undefined reference to `RAND_add'
libs/libssh.a(libcrypto.c.o): In function `sha1_init':
/home/gg/libssh/src/libcrypto.c:84: undefined reference to `EVP_MD_CTX_new'
The problem can be fixed by copying dynamic library files (libssh.so, libssh.so.4, libssh.so.4.5.0
) into the libs/
folder, but I guess the compiler will link with dynamic library in this case.
libssh.so, libssh.so.4, libssh.so.4.5.0
libs/
Can somebody tell me the proper way to link libssh static library? Thank you !!
Something extra (optional):
Actually, I was trying to build an ssh server application using includeOS, I try to link dynamic library with it by adding target_link_libraries
into the cmakelist.txt
, and I got an error usr/bin/ld unrecognized option "-Wl,-rpath,path_to_my_sshlib"
when I make
it. I guess may be unikernel can not support dynamic linking, because includeOS only has one static libray path variable in cmakelist
target_link_libraries
cmakelist.txt
usr/bin/ld unrecognized option "-Wl,-rpath,path_to_my_sshlib"
make
----------------------Edit--------------------------------
One of the error message:
`/home/gavin/libssh/src/wrapper.c:156: undefined reference to `BN_clear_free'`
wrapper.c, line 156:
bignum_free(crypto->e);
it was defined in libssh/libcrypto.h
which included by wrapper.h
libssh/libcrypto.h
wrapper.h
libcrypto.h line 70:
#define bignum_free(num) BN_clear_free(num)
And I notice that void BN_clear_free(BIGNUM *a);
is a function defined in openssl library
void BN_clear_free(BIGNUM *a);
Could introducing another library cause the problem?
if so, how could I fix it?
why dynamic linking dose not have this issue?
gcc sshtest.c -Llibs -lssh -static -o sshtest
gcc sshtest.c libs/libssh.a -o sshtest
@joyallen , both of them result to the same error, do you have any idea why the error generated at the path of my cloned repository? thank you
– NoobieG
Jun 28 at 16:01
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try
gcc sshtest.c -Llibs -lssh -static -o sshtest
, Orgcc sshtest.c libs/libssh.a -o sshtest
.– Joy Allen
Jun 28 at 7:08