link libssh with static library (libssh.a)

狂风中的少年 提交于 2019-12-10 12:07:57

问题


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/

Then compile it with command 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.

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

----------------------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

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

Could introducing another library cause the problem? if so, how could I fix it? why dynamic linking dose not have this issue?

来源:https://stackoverflow.com/questions/51074521/link-libssh-with-static-library-libssh-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!