title: Undefined symbols for architecture x86_64 (clang)
body: <p>I'm trying to use OpenSSL to compute sha1 hash from a c program. I am compiling with clang on Mac OS X Yosemite with an Intel i7 (so 64 bit).</p>
  
  <p>The relevant piece of code is roughly like so:</p>
  
  <pre><code>#include &lt;openssl/evp.h&gt;
  ...
  unsigned char outHash[20]; 
  hash("SHA1","abcd", 20, outHash);
  </code></pre>
  
  <p>The thing is, when using the "hash" function from <code>openssl/evp.h</code>, compiling with clang yields the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>Undefined symbols for architecture x86_64:
    "_hash", referenced from:
        _getRandomSHA1 in main-68ccd6.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>So it looks like OpenSSL is not found by the linker (the "hash" function is unidentified). Any ideas on how to fix this? </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It turns out that I was trying to use a function that does not exist ("hash") - sorry for misleading you about that. </p>
  
  <p>However I'm still having quite the same problem: including <code>openssl/evp.h</code> does not seem to work. </p>
  
  <p>This is the hash function that I am using, it uses evp to perform sha1 encoding:</p>
  
  <pre><code>unsigned int hash(const char *mode, const char* dataToHash, size_t dataSize, unsigned char* outHashed) {
      unsigned int md_len = -1;
      const EVP_MD *md = EVP_get_digestbyname(mode);
      if(NULL != md) {
          EVP_MD_CTX mdctx;
          EVP_MD_CTX_init(&amp;mdctx);
          EVP_DigestInit_ex(&amp;mdctx, md, NULL);
          EVP_DigestUpdate(&amp;mdctx, dataToHash, dataSize);
          EVP_DigestFinal_ex(&amp;mdctx, outHashed, &amp;md_len);
          EVP_MD_CTX_cleanup(&amp;mdctx);
      }
      return md_len;
  }
  </code></pre>
  
  <p>And then I call:</p>
  
  <pre><code>hash("SHA1","abcd", 20, outHash);
  </code></pre>
  
  <p>I am compiling same as before, this is my compile command (pretty simple):</p>
  
  <pre><code> clang main.c
  </code></pre>
  
  <p>And I'm getting the following error from the linker:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "_EVP_DigestFinal_ex", referenced from:
        _hash in main-935849.o
    "_EVP_DigestInit_ex", referenced from:
        _hash in main-935849.o
    "_EVP_DigestUpdate", referenced from:
        _hash in main-935849.o
    "_EVP_MD_CTX_cleanup", referenced from:
        _hash in main-935849.o
    "_EVP_MD_CTX_init", referenced from:
        _hash in main-935849.o
    "_EVP_get_digestbyname", referenced from:
        _hash in main-935849.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>