使用针对OpenSSL的静态和动态库的链接进行编译 [英] Compile with linking against static and dynamic library for OpenSSL
问题描述
我想编译我的代码,包括OpenSSL库.出于我的目的,有必要静态链接库 .
I want to compile my code including the OpenSSL library. For my purpose it is necessary to link the library statically.
如果我是动态链接脚本以进行编译,则外观类似于g++ test.cpp -lcrypto -o test
.
If I was dynamically linking the script for compilation would look like g++ test.cpp -lcrypto -o test
.
我尝试使用-static
选项进行编译,但是如果这样做,则会出现以下错误:
I tried to use the -static
option for the compilation, but if i do so i get the following error:
/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
我该如何解决这个问题?
How can i solve this problem?
推荐答案
g++ test.cpp -lcrypto -o test
尝试:
g++ test.cpp -o test -lcrypto -ldl
库的顺序和位置.
出于我的目的,必须静态链接库.
For my purpose it is necessary to link the library statically.
我的坏.我错过了这个.为此,它最容易执行:
My bad. I missed this earlier. For this, its easiest to perform:
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,OS X和iOS不支持它们).
Some folks want to use -Bstatic
, L
and -l
. But that's not portable (BSDs, OS X and iOS don't honor them).
档案只是目标文件(*.o
)的集合.因此,您可以在需要目标文件的任何地方使用存档.
An archive is just a collection of object files (*.o
). So you can use the archive anywhere a object file is needed.
您可以通过以下方式找到这些库:
You can locate the libraries with:
$ lsb_release -r
Release: 14.04
$ find /usr/lib -iname libcrypto.a
/usr/lib/x86_64-linux-gnu/libcrypto.a
此外,test
是一个真实的程序.确保使用./test
运行它.为了避免发生冲突,我通常将其命名为test.exe
.
Also, test
is a real program. Be sure to run it with ./test
. I usually name it test.exe
to avoid collisions.
如果我将lib放在输出文件的前面或后面有什么区别?
what is the difference if i place the lib in front of the output file or after it?
对此进行设想的方法是将其转换(有些弃权):
The way to envision this is to convert this (with some hand waiving):
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
输出文件(-o test
)的位置无关.我用它来分隔对象(在左侧)和库(在右侧).
The location of the output file (-o test
) is not relevant. I use it to separate object (on the left) and libraries (on the right).
这篇关于使用针对OpenSSL的静态和动态库的链接进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!