Compile with linking against static and dynamic library for OpenSSL

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

I want to compile my code including the OpenSSL library. For my purpose it is necessary to link the library statically.

If I was dynamically linking the script for compilation would look like g++ test.cpp -lcrypto -o test.

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?

回答1:

g++ test.cpp -lcrypto -o test 

Try:

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

Order and placement of the library matter.


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 

Some folks want to use -Bstatic, L and -l. But that's not portable (BSDs, OS X and iOS don't honor them).

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 

Also, test is a real program. Be sure to run it with ./test. I usually name it test.exe to avoid collisions.


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 

Into:

g++ test.o libcrypto.o libssl.o -o test -ldl 

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



回答2:

I'm not sure if I understand exactly what you're trying to do, but have you tried replacing -lcrypto -ldl with /path/to/libcrypto.a /path/to/libdl.a?



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!