How to use static linking with OpenSSL in C/C++
And its all ok but only, when you have Openssl installed. I would like to compile it that I can run them on OS where theres no Openssl installend (linux-like OS). I tried this:
gcc -c openssltest.c -lcrypto -static gcc openssltest.o -o openssltest -lcrypto -static
g++ -c openssltest.cpp -lcrypto -static g++ openssltest.o -o openssltest -lcrypto -static
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x19): undefined reference to `dlopen' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x2c): undefined reference to `dlsym' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': (.text+0x37): undefined reference to `dlclose' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func': (.text+0x354): undefined reference to `dlsym' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func': (.text+0x3fb): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var': (.text+0x474): undefined reference to `dlsym' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var': (.text+0x52e): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': (.text+0x5a2): undefined reference to `dlopen' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': (.text+0x60b): undefined reference to `dlclose' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load': (.text+0x638): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr': (.text+0x6cd): undefined reference to `dladdr' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr': (.text+0x731): undefined reference to `dlerror' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_unload': (.text+0x78a): undefined reference to `dlclose' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_free': (.text+0x4d): undefined reference to `inflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_free': (.text+0x6b): undefined reference to `deflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_ctrl': (.text+0x284): undefined reference to `deflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_ctrl': (.text+0x342): undefined reference to `zError' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_expand_block': (.text+0x411): undefined reference to `inflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_compress_block': (.text+0x4ca): undefined reference to `deflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_finish': (.text+0x51f): undefined reference to `inflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_finish': (.text+0x528): undefined reference to `deflateEnd' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_init': (.text+0x5d7): undefined reference to `inflateInit_' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `zlib_stateful_init': (.text+0x659): undefined reference to `deflateInit_' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': (.text+0x893): undefined reference to `inflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': (.text+0x90d): undefined reference to `zError' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_read': (.text+0x97c): undefined reference to `inflateInit_' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': (.text+0xa6f): undefined reference to `deflate' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': (.text+0xaec): undefined reference to `zError' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_write': (.text+0xb7e): undefined reference to `deflateInit_' collect2: error: ld returned 1 exit status
How can I make this? I remember that a while ago I did this but now, erm, forgot how to do it. Im on Ubuntu 13.04 x64 — can it be an issue?
Creating a link to a shared library
I have a library ( ZendDebugger.so ) which in turn requires the openssl library but it doesn’t find a reference to it. Indeed when ZendDebugger.so loads, it prints this error on the error log ( which is the Apache error_log file in my case ):
Failed loading /usr/lib64/php/7.1/modules/ZendDebugger.so: libssl.so.1.0.0: cannot open shared object file: No such file or directory
sudo yum install openssl Loaded plugins: priorities, update-motd, upgrade-helper amzn-main | 2.1 kB 00:00:00 amzn-updates | 2.3 kB 00:00:00 Package 1:openssl-1.0.1k-15.99.amzn1.x86_64 already installed and latest version Nothing to do
Looks like there is already a custom openssl installed, in this case an Amazon version. So i may want to do something like:
cd /usr/lib64 ln /path/to/amazon/openssl/openssl-1.0.1k-15.99.amzn1.x86_64.so libssl.so
In that way i would create a shared library that gets automatically seen by ZendDebugger.so, since it would be created in /usr/lib64 which is one of the default directories where the executables go to look for their libraries. But before that, i need to know the path for the openssl-1.0.1k-15.99.amzn1.x86_64 file :
sudo find / openssl-1.0.1k-15.99.amzn1.x86_64 . . find: `openssl-1.0.1k-15.99.amzn1.x86_64': No such file or directory
What am i missing? How can i find the path for the Amazon openssl ? Or do you have any other solution? Thanks
2 Answers 2
RPMs are always split up into areas of concern. They’re typically named:
Where would be openssl and will i686 or x86_64 . There are other architectures but these are the common ones on AMD/Intel hardware so we’ll stick to just those for this discussion.
The actual tools that comprise the openssl software are included in the RPM openssl.x86_64 . Notice how the name of this RPM conforms to the patterns I showed above?
The libraries (.so) are in this RPM, openssl-libs.x86_64 while the header files (.h) are in this RPM, openssl-devel.x86_64 .
So your issue is you haven’t actually installed the library or header files so that your package can compile and link against the openssl package.
$ yum search openssl | grep ^openssl openssl-devel.i686 : Files for development of applications which will use openssl-devel.x86_64 : Files for development of applications which will use openssl-perl.x86_64 : Perl scripts provided with OpenSSL openssl-pkcs11.x86_64 : A PKCS#11 engine for use with OpenSSL openssl-static.i686 : Libraries for static linking of applications which will openssl-static.x86_64 : Libraries for static linking of applications which will openssl.x86_64 : Utilities from the general purpose cryptography library with openssl-libs.x86_64 : A general purpose cryptography library with TLS openssl-libs.i686 : A general purpose cryptography library with TLS openssl098e.i686 : A compatibility version of a general cryptography and TLS openssl098e.x86_64 : A compatibility version of a general cryptography and TLS
$ sudo yum install -y openssl-
Once installed you can confirm the contents of the openssl-libs RPM:
$ rpm -ql openssl-libs | grep libssl.so /usr/lib64/.libssl.so.1.0.2k.hmac /usr/lib64/.libssl.so.10.hmac /usr/lib64/libssl.so.1.0.2k /usr/lib64/libssl.so.10
What if I don’t know what RPM to install?
This is a common question that I see all the time. Most people that are either new to Redhat distros such as Fedora/CentOS/RHEL are unaware of the repoquery command line tool. With this tool you can query remote YUM repositories and look for what packages provide a given file.
$ repoquery -qf */libssl.so* openssl-libs-1:1.0.2k-12.el7.x86_64 openssl-devel-1:1.0.2k-12.el7.x86_64 openssl098e-0:0.9.8e-29.el7.centos.3.i686 openssl-libs-1:1.0.2k-12.el7.i686 openssl-devel-1:1.0.2k-12.el7.i686
We can see that the libssl.so* file is included with all the above RPMs. So based on your existing RPM, you’d likely want these 2:
openssl-libs-1:1.0.2k-12.el7.x86_64 openssl-devel-1:1.0.2k-12.el7.x86_64
NOTE: Keep in mind that I’m showing you CentOS 7.x packages, you’re using Amazon’s AMI which is their own branded package of CentOS, so the names are slightly different, but the process I’ve shown you should be identical, regardless.
Linking issues using OpenSSL in Ubuntu
I have installed OpenSSL using sudo apt-get install openssl-dev . When I try to compile it using Netbeans it gives following errors. How can I fix this problem?
g++ -lssl -o dist/Debug/GNU-Linux-x86/cppapplication_2 build/Debug/GNU-Linux-x86/main.o -L/home/sercan/Desktop/openssl-0.9.8h-1-lib/lib build/Debug/GNU-Linux-x86/main.o: In function `main': /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:46: undefined reference to `OPENSSL_add_all_algorithms_noconf' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:48: undefined reference to `ERR_load_crypto_strings' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:58: undefined reference to `d2i_PKCS12_fp' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:66: undefined reference to `ERR_print_errors_fp' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:72: undefined reference to `PKCS12_parse' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:76: undefined reference to `ERR_print_errors_fp' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:82: undefined reference to `PKCS12_free' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:96: undefined reference to `PEM_write_PrivateKey' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:104: undefined reference to `PEM_write_X509_AUX' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:108: undefined reference to `sk_num' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:114: undefined reference to `sk_value' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:114: undefined reference to `PEM_write_X509_AUX' /home/sercan/NetBeansProjects/CppApplication_2/main.cpp:112: undefined reference to `sk_num'
#include #include #include #include #include /* Simple PKCS#12 file reader */ int main(int argc, char **argv) < FILE *fp; EVP_PKEY *pkey; X509 *cert; STACK_OF(X509) *ca = NULL; PKCS12 *p12; int i; if (argc != 4) < fprintf(stderr, "Usage: pkread p12file password opfile\n"); exit(1); >OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); if (!(fp = fopen(argv[1], "rb"))) < fprintf(stderr, "Error opening file %s\n", argv[1]); exit(1); >p12 = d2i_PKCS12_fp(fp, NULL); fclose(fp); if (!p12) < fprintf(stderr, "Error reading PKCS#12 file\n"); ERR_print_errors_fp(stderr); exit(1); >if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) < fprintf(stderr, "Error parsing PKCS#12 file\n"); ERR_print_errors_fp(stderr); exit(1); >PKCS12_free(p12); if (!(fp = fopen(argv[3], "w"))) < fprintf(stderr, "Error opening file %s\n", argv[1]); exit(1); >if (pkey) < fprintf(fp, "***Private Key***\n"); PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL); >if (cert) < fprintf(fp, "***User Certificate***\n"); PEM_write_X509_AUX(fp, cert); >if (ca && sk_X509_num(ca)) < fprintf(fp, "***Other Certificates***\n"); for (i = 0; i < sk_X509_num(ca); i++) PEM_write_X509_AUX(fp, sk_X509_value(ca, i)); >fclose(fp); return 0; >