简体   繁体   English

将 libssh 与静态库(libssh.a)链接起来

[英]link libssh with static library (libssh.a)

I was trying to link my program with libssh static library.我试图将我的程序与 libssh 静态库链接起来。

Following is my simple code copied from libssh tutorial:以下是我从 libssh 教程中复制的简单代码:

//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?有人可以告诉我链接 libssh 静态库的正确方法吗? 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.其实,我是想建立使用includeOS SSH服务器应用程序,我试图与它链接的动态库中加入target_link_librariescmakelist.txt ,我得到了一个错误usr/bin/ld unrecognized option "-Wl,-rpath,path_to_my_sshlib"当我make它。 I guess may be unikernel can not support dynamic linking, because includeOS only has one static libray path variable in cmakelist我猜可能是unikernel不支持动态链接,因为includeOS在cmakelist中只有一个静态库路径变量

----------------------Edit-------------------------------- - - - - - - - - - - - 编辑 - - - - - - - - - - - - - - -----

One of the error message:错误信息之一:

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

wrapper.c, line 156: wrapper.c,第 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: libcrypto.h 第 70 行:

#define bignum_free(num) BN_clear_free(num)

And I notice that void BN_clear_free(BIGNUM *a);我注意到void BN_clear_free(BIGNUM *a); is a function defined in openssl library是 openssl 库中定义的函数

Could introducing another library cause the problem?引入另一个库会导致问题吗? if so, how could I fix it?如果是这样,我该如何解决? why dynamic linking dose not have this issue?为什么动态链接没有这个问题?

  1. Install libssl-dev , cmake and maybe some other dependencies if you don't have them already.安装libssl-devcmake以及其他一些依赖项(如果您还没有的话)。
  2. Build libssh from source with static flags.使用静态标志从源代码构建 libssh。

    • eg extract libssh-0.9.3.tar.xz to /home/user/libssh-0.9.3例如将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
    • now you can use /home/user/libssh-0.9.3/build/src/libssh.a现在你可以使用/home/user/libssh-0.9.3/build/src/libssh.a
  3. Compile your code with 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 -staticgcc -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编译你的代码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

  4. You'll still get some warnings about shared libraries from the glibc, but resulted binary is completely static.您仍然会从 glibc 收到一些关于共享库的警告,但生成的二进制文件是完全静态的。

Presumably you'll need to change include order in your source to 1) stdlib.h, stdio.h etc, 2) libssh/libssh.h.据推测,您需要将源代码中的包含顺序更改为 1) stdlib.h、stdio.h 等,2) libssh/libssh.h。 And remove #define LIBSSH_STATIC 1并删除#define LIBSSH_STATIC 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM