title: OpenSSL linking undefined reference to `EVP_MD_CTX_new' and others
body: <p>I am compiling some codes based on OpenSSL. The compiler, parameters and error are shown in the following.</p>
  
  <pre><code>$ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lssl -lcrypto -Wall -O3 -Wpedantic -Wno-unused-result -Wno-pointer-sign ot.c -o ot
  /tmp/cck3sgve.o: In function `main':
  ot.c:(.text.startup+0x20): undefined reference to     `EVP_get_digestbyname'
  ot.c:(.text.startup+0x31): undefined reference to `EVP_MD_CTX_new'
  ot.c:(.text.startup+0x41): undefined reference to `EVP_DigestInit_ex'
  ot.c:(.text.startup+0x53): undefined reference to `EVP_DigestFinal_ex'
  ot.c:(.text.startup+0x5b): undefined reference to `EVP_MD_CTX_free'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>Here are versions of components in the compile environments.</p>
  
  <pre><code>$ uname -a
  Linux yzx-lab3 4.4.0-17763-Microsoft #379-Microsoft Wed Mar 06 19:16:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
  $ /home/share/pkp/bin/openssl version
  ./bin/openssl: relocation error: ./bin/openssl: symbol EVP_mdc2 version OPENSSL_1_1_0 not defined in file libcrypto.so.1.1 with link time reference
  $ grep "OPENSSL_VERSION" /home/share/pkp/openssl-1.1.1a/include/openssl/opensslv.h
  # define OPENSSL_VERSION_NUMBER  0x1010101fL
  # define OPENSSL_VERSION_TEXT    "OpenSSL 1.1.1a  20 Nov 2018"
  $ gcc --version
  gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
  Copyright (C) 2017 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  </code></pre>
  
  <p>I know there already many question about the similar error and have tried many solutions. But they do not work. </p>
  
  <p>1) Fist I obtain the source code and unzip all the fils to "/home/share/pkp".</p>
  
  <pre><code>wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1a.tar.gz
  </code></pre>
  
  <p>Then compile and install openssl library.</p>
  
  <pre><code>./config --prefix=/home/share/pkp --openssldir=/home/share/pkp/openssl
      make &amp;&amp; make test &amp;&amp; make install
  </code></pre>
  
  <p>There is no error message during the installation.</p>
  
  <p>2) To asure that openssl is compiled and installed correctly, I do some verifications.</p>
  
  <pre><code>$ ls /home/share/pkp/lib/ -l
  total 14912
  drwxrwxrwx 1 share share    4096 Jun 18 12:30 engines-1.1
  -rw-r--r-- 1 share share 5700252 Jun 18 12:30 libcrypto.a
  lrwxrwxrwx 1 share share      16 Jun 18 12:30 libcrypto.so -&gt; libcrypto.so.1.1
  -rwxr-xr-x 1 share share 3414744 Jun 18 12:30 libcrypto.so.1.1
  -rw-r--r-- 1 share share 1043216 Jun 18 12:30 libssl.a
  lrwxrwxrwx 1 share share      13 Jun 18 12:30 libssl.so -&gt; libssl.so.1.1
  -rwxr-xr-x 1 share share  692696 Jun 18 12:30 libssl.so.1.1
  $ nm /home/share/pkp/lib/libcrypto.a | grep "EVP_get_digestbyname" | wc -l
  24
  $ du /home/share/pkp/include/openssl -ah
  4.0K    /home/share/pkp/include/openssl/aes.h
  36K     /home/share/pkp/include/openssl/asn1.h
  0       /home/share/pkp/include/openssl/asn1_mac.h
  16K     /home/share/pkp/include/openssl/asn1err.h
  36K     /home/share/pkp/include/openssl/asn1t.h
  4.0K    /home/share/pkp/include/openssl/async.h
  4.0K    /home/share/pkp/include/openssl/asyncerr.h
  36K     /home/share/pkp/include/openssl/bio.h
  8.0K    /home/share/pkp/include/openssl/bioerr.h
  4.0K    /home/share/pkp/include/openssl/blowfish.h
  24K     /home/share/pkp/include/openssl/bn.h
  8.0K    /home/share/pkp/include/openssl/bnerr.h
  4.0K    /home/share/pkp/include/openssl/buffer.h
  4.0K    /home/share/pkp/include/openssl/buffererr.h
  4.0K    /home/share/pkp/include/openssl/camellia.h
  4.0K    /home/share/pkp/include/openssl/cast.h
  4.0K    /home/share/pkp/include/openssl/cmac.h
  20K     /home/share/pkp/include/openssl/cms.h
  12K     /home/share/pkp/include/openssl/cmserr.h
  4.0K    /home/share/pkp/include/openssl/comp.h
  4.0K    /home/share/pkp/include/openssl/comperr.h
  8.0K    /home/share/pkp/include/openssl/conf.h
  4.0K    /home/share/pkp/include/openssl/conf_api.h
  4.0K    /home/share/pkp/include/openssl/conferr.h
  20K     /home/share/pkp/include/openssl/crypto.h
  4.0K    /home/share/pkp/include/openssl/cryptoerr.h
  16K     /home/share/pkp/include/openssl/ct.h
  4.0K    /home/share/pkp/include/openssl/cterr.h
  8.0K    /home/share/pkp/include/openssl/des.h
  16K     /home/share/pkp/include/openssl/dh.h
  4.0K    /home/share/pkp/include/openssl/dherr.h
  12K     /home/share/pkp/include/openssl/dsa.h
  4.0K    /home/share/pkp/include/openssl/dsaerr.h
  4.0K    /home/share/pkp/include/openssl/dtls1.h
  12K     /home/share/pkp/include/openssl/e_os2.h
  4.0K    /home/share/pkp/include/openssl/ebcdic.h
  64K     /home/share/pkp/include/openssl/ec.h
  0       /home/share/pkp/include/openssl/ecdh.h
  0       /home/share/pkp/include/openssl/ecdsa.h
  16K     /home/share/pkp/include/openssl/ecerr.h
  36K     /home/share/pkp/include/openssl/engine.h
  8.0K    /home/share/pkp/include/openssl/engineerr.h
  12K     /home/share/pkp/include/openssl/err.h
  76K     /home/share/pkp/include/openssl/evp.h
  12K     /home/share/pkp/include/openssl/evperr.h
  4.0K    /home/share/pkp/include/openssl/hmac.h
  4.0K    /home/share/pkp/include/openssl/idea.h
  8.0K    /home/share/pkp/include/openssl/kdf.h
  4.0K    /home/share/pkp/include/openssl/kdferr.h
  8.0K    /home/share/pkp/include/openssl/lhash.h
  4.0K    /home/share/pkp/include/openssl/md2.h
  4.0K    /home/share/pkp/include/openssl/md4.h
  4.0K    /home/share/pkp/include/openssl/md5.h
  4.0K    /home/share/pkp/include/openssl/mdc2.h
  12K     /home/share/pkp/include/openssl/modes.h
  216K    /home/share/pkp/include/openssl/obj_mac.h
  8.0K    /home/share/pkp/include/openssl/objects.h
  4.0K    /home/share/pkp/include/openssl/objectserr.h
  16K     /home/share/pkp/include/openssl/ocsp.h
  4.0K    /home/share/pkp/include/openssl/ocsperr.h
  8.0K    /home/share/pkp/include/openssl/opensslconf.h
  8.0K    /home/share/pkp/include/openssl/opensslv.h
  8.0K    /home/share/pkp/include/openssl/ossl_typ.h
  16K     /home/share/pkp/include/openssl/pem.h
  0       /home/share/pkp/include/openssl/pem2.h
  8.0K    /home/share/pkp/include/openssl/pemerr.h
  12K     /home/share/pkp/include/openssl/pkcs12.h
  4.0K    /home/share/pkp/include/openssl/pkcs12err.h
  12K     /home/share/pkp/include/openssl/pkcs7.h
  8.0K    /home/share/pkp/include/openssl/pkcs7err.h
  4.0K    /home/share/pkp/include/openssl/rand.h
  8.0K    /home/share/pkp/include/openssl/rand_drbg.h
  8.0K    /home/share/pkp/include/openssl/randerr.h
  4.0K    /home/share/pkp/include/openssl/rc2.h
  4.0K    /home/share/pkp/include/openssl/rc4.h
  4.0K    /home/share/pkp/include/openssl/rc5.h
  4.0K    /home/share/pkp/include/openssl/ripemd.h
  24K     /home/share/pkp/include/openssl/rsa.h
  12K     /home/share/pkp/include/openssl/rsaerr.h
  8.0K    /home/share/pkp/include/openssl/safestack.h
  4.0K    /home/share/pkp/include/openssl/seed.h
  4.0K    /home/share/pkp/include/openssl/sha.h
  4.0K    /home/share/pkp/include/openssl/srp.h
  4.0K    /home/share/pkp/include/openssl/srtp.h
  112K    /home/share/pkp/include/openssl/ssl.h
  0       /home/share/pkp/include/openssl/ssl2.h
  16K     /home/share/pkp/include/openssl/ssl3.h
  48K     /home/share/pkp/include/openssl/sslerr.h
  4.0K    /home/share/pkp/include/openssl/stack.h
  12K     /home/share/pkp/include/openssl/store.h
  8.0K    /home/share/pkp/include/openssl/storeerr.h
  4.0K    /home/share/pkp/include/openssl/symhacks.h
  72K     /home/share/pkp/include/openssl/tls1.h
  24K     /home/share/pkp/include/openssl/ts.h
  8.0K    /home/share/pkp/include/openssl/tserr.h
  4.0K    /home/share/pkp/include/openssl/txt_db.h
  16K     /home/share/pkp/include/openssl/ui.h
  4.0K    /home/share/pkp/include/openssl/uierr.h
  4.0K    /home/share/pkp/include/openssl/whrlpool.h
  44K     /home/share/pkp/include/openssl/x509.h
  32K     /home/share/pkp/include/openssl/x509_vfy.h
  8.0K    /home/share/pkp/include/openssl/x509err.h
  36K     /home/share/pkp/include/openssl/x509v3.h
  12K     /home/share/pkp/include/openssl/x509v3err.h
  1.6M    /home/share/pkp/include/openssl
  </code></pre>
  
  <p>3)Finally, I tried other compile commands, but then still do not work.</p>
  
  <pre><code>$ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lcrypto -lssl -Wall -O3 -Wpedantic -Wno-unuse
  d-result -Wno-pointer-sign ot.c -o ot
  $ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lssl -lcrypto -Wall -O3 -Wpedantic -Wno-unuse
  d-result -Wno-pointer-sign ot.c -o ot
  $ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lcrypto -lssl -Wall -Wpedantic -Wno-unuse
  d-result -Wno-pointer-sign ot.c -o ot
  $ gcc -I/home/share/pkp/include -L/home/share/pkp/lib -lcrypto -O3 -Wall -Wpedantic -Wno-unuse
  d-result -Wno-pointer-sign ot.c -o ot
  </code></pre>
  
  <p>4) It seems that the openssl from downloaded source code is conflict with the openssl installed by "apt-get". So I try another method. </p>
  
  <pre><code>./config &amp;&amp; make &amp;&amp; make test &amp;&amp; make install
  </code></pre>
  
  <p>It will install openssl into the system directory. Then I run </p>
  
  <pre><code>$ gcc -lssl -lcrypto ot.c -o ot
  /tmp/ccrFwoa7.o: In function `main':
  ot.c:(.text+0x2d): undefined reference to `EVP_get_digestbyname'
  ot.c:(.text+0x66): undefined reference to `EVP_MD_CTX_new'
  ot.c:(.text+0x82): undefined reference to `EVP_DigestInit_ex'
  ot.c:(.text+0x99): undefined reference to `EVP_DigestFinal_ex'
  ot.c:(.text+0xa5): undefined reference to `EVP_MD_CTX_free'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>It still failed. Envn I follow the solution in <a href="https://stackoverflow.com/questions/18185618/how-to-use-static-linking-with-openssl-in-c-c">here</a> trying to statically link libcrypto and libssl. It failed again.</p>
  
  <pre><code>$ gcc -lssl -lcrypto -ldl -lz -static -static-libgcc ot.c -o ot
  /tmp/cc39C5Gq.o: In function `main':
  ot.c:(.text+0x2d): undefined reference to `EVP_get_digestbyname'
  ot.c:(.text+0x66): undefined reference to `EVP_MD_CTX_new'
  ot.c:(.text+0x82): undefined reference to `EVP_DigestInit_ex'
  ot.c:(.text+0x99): undefined reference to `EVP_DigestFinal_ex'
  ot.c:(.text+0xa5): undefined reference to `EVP_MD_CTX_free'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I am sure all the static libraries are in the correct location.</p>
  
  <pre><code>$ whereis lib{ssl,crypto,z,dl}.a
  libssl: /usr/lib/x86_64-linux-gnu/libssl.a /usr/lib/x86_64-linux-gnu/libssl.so /usr/local/lib/libssl.a /usr/local/lib/libssl.so
  libcrypto: /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/lib/x86_64-linux-gnu/libcrypto.so /usr/local/lib/libcrypto.a /usr/local/lib/libcrypto.so /mnt/c/Windows/System32/libcrypto.dll
  libz: /usr/lib/x86_64-linux-gnu/libz.a /usr/lib/x86_64-linux-gnu/libz.so
  libdl: /usr/lib/x86_64-linux-gnu/libdl.a /usr/lib/x86_64-linux-gnu/libdl.so
  </code></pre>
  
  <p>5) Come here to post this question.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;openssl/evp.h&gt;
  
  int main(int argc, char *argv[]){
  
  EVP_MD_CTX *mdctx;
  const EVP_MD *md;
  unsigned char md_value[EVP_MAX_MD_SIZE];
  int md_len, i;
  
  md = EVP_get_digestbyname(argv[1]);
  if(!md) {
      printf("Unknown message digest %s\n", argv[1]);
      exit(1);
  }
  
  mdctx = EVP_MD_CTX_new();
  EVP_DigestInit_ex(mdctx, md, NULL);
  EVP_DigestFinal_ex(mdctx, md_value, &amp;md_len);
  EVP_MD_CTX_free(mdctx);
  
  printf("Digest is: ");
  for (i = 0; i &lt; md_len; i++)
      printf("%02x", md_value[i]);
  printf("\n");
  
  exit(0);
  }
  </code></pre>
  
  <p>The code should be compiled with no error. But now it shows "undefined reference to `EVP_get_digestbyname'".</p>