Ubuntu 针对OpenSSL的静态和动态库进行链接编译

Ubuntu 针对OpenSSL的静态和动态库进行链接编译,ubuntu,openssl,Ubuntu,Openssl,我想编译我的代码,包括OpenSSL库。出于我的目的,有必要静态链接库 如果我动态链接用于编译的脚本,它看起来像g++test.cpp-lcrypto-o test 我尝试使用-static选项进行编译,但如果这样做,则会出现以下错误: /usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup': (.text+0x15): N

我想编译我的代码,包括OpenSSL库。出于我的目的,有必要静态链接库

如果我动态链接用于编译的脚本,它看起来像
g++test.cpp-lcrypto-o test

我尝试使用
-static
选项进行编译,但如果这样做,则会出现以下错误:

/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x15): Nicht definierter Verweis auf `dlopen'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x2b): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_globallookup':
(.text+0x35): Nicht definierter Verweis auf `dlclose'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_func':
(.text+0x381): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_func':
(.text+0x460): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_var':
(.text+0x4e1): Nicht definierter Verweis auf `dlsym'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_bind_var':
(.text+0x5c0): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x630): Nicht definierter Verweis auf `dlopen'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x6a0): Nicht definierter Verweis auf `dlclose'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_load':
(.text+0x6df): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_pathbyaddr':
(.text+0x788): Nicht definierter Verweis auf `dladdr'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_pathbyaddr':
(.text+0x7d9): Nicht definierter Verweis auf `dlerror'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libcrypto.a(dso_dlfcn.o): In Funktion `dlfcn_unload':
(.text+0x834): Nicht definierter Verweis auf `dlclose'
collect2: error: ld returned 1 exit status
我怎样才能解决这个问题

尝试:

图书馆事务的顺序和位置


出于我的目的,有必要静态链接库

我的错。我之前错过了这个。为此,最容易执行以下操作:

g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl
有些人想使用
-Bstatic
L
-L
。但这是不可移植的(BSD、OSX和iOS不尊重它们)

归档只是对象文件的集合(
*.o
)。因此,您可以在需要对象文件的任何位置使用归档

您可以通过以下方式找到库:

$ lsb_release -r
Release:    14.04
$ find /usr/lib -iname libcrypto.a
/usr/lib/x86_64-linux-gnu/libcrypto.a
而且,
test
是一个真正的程序。确保使用
/test
运行它。我通常将其命名为
test.exe
,以避免冲突



如果将库放在输出文件的前面或后面,有什么区别

设想这一点的方法是将其转化(放弃一些手):

进入:


输出文件的位置(
-o test
)不相关。我用它来分隔对象(左侧)和库(右侧)。

我不确定我是否完全理解您的意图,但您是否尝试过用
/path/to/libcrypto.a/path/to/libdl.a
替换
-lcrypto-ldl

@Sacx实际上这些答案都很不令人满意。如果遵循第一个答案,则不会得到静态链接的二进制文件(与动态链接的二进制文件大小相同)。如果我尝试使用-static选项而不是-static libc,并将-ldl附加链接到-lcrypto,我会得到以下警告:/usr/lib/gcc/i686 linux gnu/4.8/../../../../../i386 linux gnu/libcrypto.a(dso_dlfcn.o):“函数中的`dlfcn\u globallookup':(.text+0x15):Warnung:在静态链接的应用程序中使用'dlopen'需要在运行时使用glibc版本中用于链接的共享库“为什么有必要链接-ldl,它似乎是动态链接器库我大约8个月前做了等效的(静态编译tcpdump)。我花了一天的时间浏览了不同的谷歌搜索结果,直到我找到了一个有效的。对不起,不,我不记得我是怎么做到的。综上所述,我投票决定将此移到堆栈溢出,因为这是一个编程问题,而不是一个安全问题。第一条评论的链接相当于堆栈溢出,@gowenfawr你能把你的解决方案放在那里吗?同样,我不记得我是如何做到的。。。我给你的建议是反复地把头撞在墙上,尝试不同的东西,直到有东西奏效。见鬼,这就是我小时候编译包的工作原理(回到
configure
)之前。如果我将lib放在输出文件之前或之后,会有什么区别@jww
g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl
$ lsb_release -r
Release:    14.04
$ find /usr/lib -iname libcrypto.a
/usr/lib/x86_64-linux-gnu/libcrypto.a
g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl
g++ test.o libcrypto.o libssl.o -o test -ldl