title: How to compile program with OpenSSL on Windows with MinGW
body: <p>I tried running this sample code from fm4dd.com. But I don't know how to actually <em>include</em> the header files into my program.
  Orignally it was like:</p>
  
  <pre><code>#include &lt;openssl/bio.h&gt;
  </code></pre>
  
  <p>But i changes it to their actual path, but an error still shows up.</p>
  
  <pre><code>#include &lt;C:\openssl\include\openssl\bio.h&gt;
  #include &lt;C:\openssl\include\openssl\err.h&gt;
  #include &lt;C:\openssl\include\openssl\pem.h&gt;
  #include &lt;C:\openssl\include\openssl\x509.h&gt;
  #include &lt;C:\openssl\include\openssl\e_os2.h&gt;
  
  int main() {
  
    const char cert_filestr[] = "./cert-file.pem";
               EVP_PKEY *pkey = NULL;
    BIO              *certbio = NULL;
    BIO               *outbio = NULL;
    X509                *cert = NULL;
    int ret;
  
    /* ---------------------------------------------------------- *
     * These function calls initialize openssl for correct work.  *
     * ---------------------------------------------------------- */
    OpenSSL_add_all_algorithms();
    ERR_load_BIO_strings();
    ERR_load_crypto_strings();
  
    /* ---------------------------------------------------------- *
     * Create the Input/Output BIO's.                             *
     * ---------------------------------------------------------- */
    certbio = BIO_new(BIO_s_file());
    outbio  = BIO_new_fp(stdout, BIO_NOCLOSE);
  
    /* ---------------------------------------------------------- *
     * Load the certificate from file (PEM).                      *
     * ---------------------------------------------------------- */
    ret = BIO_read_filename(certbio, cert_filestr);
    if (! (cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
      BIO_printf(outbio, "Error loading cert into memory\n");
      exit(-1);
    }
  
    /* ---------------------------------------------------------- *
     * Extract the certificate's public key data.                 *
     * ---------------------------------------------------------- */
    if ((pkey = X509_get_pubkey(cert)) == NULL)
      BIO_printf(outbio, "Error getting public key from certificate");
  
    /* ---------------------------------------------------------- *
     * Print the public key information and the key in PEM format *
     * ---------------------------------------------------------- */
    /* display the key type and size here */
    if (pkey) {
      switch (pkey-&gt;type) {
        case EVP_PKEY_RSA:
          BIO_printf(outbio, "%d bit RSA Key\n\n", EVP_PKEY_bits(pkey));
          break;
        case EVP_PKEY_DSA:
          BIO_printf(outbio, "%d bit DSA Key\n\n", EVP_PKEY_bits(pkey));
          break;
        default:
          BIO_printf(outbio, "%d bit non-RSA/DSA Key\n\n", EVP_PKEY_bits(pkey));
          break;
      }
    }
  
    if(!PEM_write_bio_PUBKEY(outbio, pkey))
      BIO_printf(outbio, "Error writing public key data in PEM format");
  
    EVP_PKEY_free(pkey);
    X509_free(cert);
    BIO_free_all(certbio);
    BIO_free_all(outbio);
    exit(0);
  }
  </code></pre>
  
  <p>but the following error shows up every time I try to compile it on the command prompt. Since, I'm a noob, I have no clue how to proceed from here and what to do to fix this error.</p>
  
  <pre><code>c:\openssl&gt;gcc -lssl -lcrypto -o test test.c 
  In file included from test.c:1:0:
  C:\openssl\include\openssl\bio.h:62:27: fatal error: openssl/e_os2.h: No such file or directory
  #include &lt;openssl/e_os2.h&gt;
                         ^
  compilation terminated.
  </code></pre>
  
  <p>Edit:
  I included the solution to the problem, but now a new error showed up:</p>
  
  <pre><code>c:\openssl&gt;gcc -lssl -lcrypto -o test test.c -IC:\openssl\include\
  c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lssl
  c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lcrypto
  collect2.exe: error: ld returned 1 exit status
  </code></pre>