title: How to fix makefile to properly include -lcrypto to avoid linker error: undefined reference to `EVP_CIPHER_CTX_new'
body: <p>First of all I am trying to use </p>
  
  <pre><code>#include &lt;openssl/evp.h&gt;
  </code></pre>
  
  <p>I am getting the common linker error: </p>
  
  <pre><code>undefined reference to `EVP_CIPHER_CTX_new'
  </code></pre>
  
  <p>which I know can be solved by using <code>-lcrypto</code> when compiling. I am able to do this when compiling manually i.e </p>
  
  <pre><code>g++ encrv2.cpp -o encrv2 -lcrypto 
  </code></pre>
  
  <p>though I am trying to create a Makefile and putting my encryption/decryption functions in a seperate header file (AesGcm) and am unable to get around the linker issue. Below is my Makefile so far and the output when I try and make all.</p>
  
  <pre><code>CXX = g++
  CXXFLAGS = -std=c++14
  LDFLAGS= -lcrypto
  
  all: encrv2
  
  AesGcm.o: AesGcm.cpp AesGcm.h
      $(CXX) $(CXXFLAGS) -c AesGcm.cpp $(LDFLAGS)
  
  encrv2.o: encrv2.cpp AesGcm.h
      $(CXX) $(CXXFLAGS) -c encrv2.cpp $(LDFLAGS)
  
  encrv2: encrv2.o AesGcm.o
  
  clean:
      rm -f *~ *.o encrv2 
  </code></pre>
  
  <p>Giving the following error:</p>
  
  <pre><code>cc -lcrypto  encrv2.o AesGcm.o   -o encrv2
  AesGcm.o: In function `AesGcm::encrypt(unsigned char*, int, unsigned char*, int, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':
  AesGcm.cpp:(.text+0x3e): undefined reference to `EVP_CIPHER_CTX_new'
  AesGcm.cpp:(.text+0x58): undefined reference to `EVP_aes_256_gcm'
  AesGcm.cpp:(.text+0x77): undefined reference to `EVP_EncryptInit_ex'
  AesGcm.cpp:(.text+0xae): undefined reference to `EVP_EncryptInit_ex'
  AesGcm.cpp:(.text+0xe0): undefined reference to `EVP_EncryptUpdate'
  AesGcm.cpp:(.text+0x111): undefined reference to `EVP_EncryptUpdate'
  AesGcm.cpp:(.text+0x148): undefined reference to `EVP_EncryptFinal_ex'
  AesGcm.cpp:(.text+0x17b): undefined reference to `EVP_CIPHER_CTX_ctrl'
  AesGcm.cpp:(.text+0x197): undefined reference to `EVP_CIPHER_CTX_free'
  AesGcm.o: In function `AesGcm::decrypt(unsigned char*, int, unsigned char*, int, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':
  AesGcm.cpp:(.text+0x30a): undefined reference to `EVP_CIPHER_CTX_new'
  AesGcm.cpp:(.text+0x324): undefined reference to `EVP_aes_256_gcm'
  AesGcm.cpp:(.text+0x343): undefined reference to `EVP_DecryptInit_ex'
  AesGcm.cpp:(.text+0x37a): undefined reference to `EVP_DecryptInit_ex'
  AesGcm.cpp:(.text+0x3ac): undefined reference to `EVP_DecryptUpdate'
  AesGcm.cpp:(.text+0x3dd): undefined reference to `EVP_DecryptUpdate'
  AesGcm.cpp:(.text+0x410): undefined reference to `EVP_CIPHER_CTX_ctrl'
  AesGcm.cpp:(.text+0x441): undefined reference to `EVP_DecryptFinal_ex'
  AesGcm.cpp:(.text+0x450): undefined reference to `EVP_CIPHER_CTX_free'
  collect2: error: ld returned 1 exit status
  &lt;builtin&gt;: recipe for target 'encrv2' failed
  make: *** [encrv2] Error 1
  </code></pre>