将libssh与静态库(libssh.a)链接 [英] link libssh with static library (libssh.a)

查看:647
本文介绍了将libssh与静态库(libssh.a)链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将程序与libssh静态库链接.

I was trying to link my program with libssh static library.

以下是我从libssh教程中复制的简单代码:

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);
}

我将库文件libssh.a放入子目录libs/

I put library file libssh.a into the subdirectory libs/

然后使用命令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'

可以通过将动态库文件(libssh.so, libssh.so.4, libssh.so.4.5.0)复制到libs/文件夹中来解决此问题,但是在这种情况下,我猜编译器将链接到动态库.

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静态库的正确方法吗?谢谢!!

Can somebody tell me the proper way to link libssh static library? Thank you !!

一些额外的东西(可选):

实际上,我试图使用includeOS构建ssh服务器应用程序,我尝试通过将target_link_libraries添加到cmakelist.txt来与动态库链接,并且在make时出现错误usr/bin/ld unrecognized option "-Wl,-rpath,path_to_my_sshlib". .我猜可能是unikernel无法支持动态链接,因为includeOS在cmakelist中只有一个静态libray路径变量

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

错误消息之一:

`/home/gavin/libssh/src/wrapper.c:156: undefined reference to `BN_clear_free'`

wrapper.c,第156行:

wrapper.c, line 156:

  bignum_free(crypto->e);

它是在libssh/libcrypto.h中定义的,并包含在wrapper.h

it was defined in libssh/libcrypto.h which included by wrapper.h

libcrypto.h第70行:

libcrypto.h line 70:

#define bignum_free(num) BN_clear_free(num)

我注意到void BN_clear_free(BIGNUM *a);是在openssl库中定义的函数

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?

推荐答案

  1. 安装libssl-devcmake以及其他一些依赖项如果还没有的话.
  2. 使用静态标志从源代码构建libssh.

  1. Install libssl-dev, cmake and maybe some other dependencies if you don't have them already.
  2. Build libssh from source with static flags.

  • 例如将libssh-0.9.3.tar.xz提取到/home/user/libssh-0.9.3
  • cd /home/user/libssh-0.9.3
  • mkdir build && cd build
  • cmake ../ -DWITH_EXAMPLES=OFF -DBUILD_SHARED_LIBS=OFF -DWITH_STATIC_LIB=ON
  • make
  • 现在您可以使用/home/user/libssh-0.9.3/build/src/libssh.a
  • e.g. extract libssh-0.9.3.tar.xz to /home/user/libssh-0.9.3
  • cd /home/user/libssh-0.9.3
  • mkdir build && cd build
  • cmake ../ -DWITH_EXAMPLES=OFF -DBUILD_SHARED_LIBS=OFF -DWITH_STATIC_LIB=ON
  • make
  • now you can use /home/user/libssh-0.9.3/build/src/libssh.a

使用gcc -I/home/user/libssh-0.9.3/include sshtest.c /home/user/libssh-0.9.3/build/src/libssh.a -lssh -lrt -lcrypto -lz -lpthread -ldl -o sshtest -static

您仍然会从glibc中获得关于共享库的警告,但是生成的二进制文件是完全静态的.

You'll still get some warnings about shared libraries from the glibc, but resulted binary is completely static.

大概您需要将源中的包含顺序更改为1)stdlib.h,stdio.h等,2)libssh/libssh.h. 然后删除#define LIBSSH_STATIC 1

Presumably you'll need to change include order in your source to 1) stdlib.h, stdio.h etc, 2) libssh/libssh.h. And remove #define LIBSSH_STATIC 1

这篇关于将libssh与静态库(libssh.a)链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆