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

Compile with linking against static and dynamic library for OpenSSL

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

如果我动态 link 编译脚本看起来像 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 -lcrypto -o test

尝试:

g++ test.cpp -o test -lcrypto -ldl

图书馆的顺序和位置很重要。


For my purpose it is necessary to link the library statically.

我的错。我早些时候错过了这个。为此,它最容易执行:

g++ test.cpp /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libssl.a -o test -ldl

有些人想使用 -BstaticL-l。但这不是可移植的(BSD、OS X 和 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以避免冲突。


what is the difference if i place the lib in front of the output file or after it?

设想这个的方法是转换这个(用一些手放弃):

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) 不相关。我用它来分隔对象(左侧)和库(右侧)。

我不确定我是否完全理解你想要做什么,但你是否尝试过将 -lcrypto -ldl 替换为 /path/to/libcrypto.a /path/to/libdl.a