title: Undefined symbols for architecture i386 for OpenSSL library
body: <p>I'm reading this book <code>Network Security with OpenSSL</code> by <code>Pravir Chandra, Matt Messier and John Viega</code> and I'm trying to create an SSL client/server connection after their example in the book using OpenSSL but I get this error and I don't know why.</p>
  
  <p>This is my server:</p>
  
  <pre><code>#include &lt;openssl/bio.h&gt;
  #include &lt;openssl/err.h&gt;
  #include &lt;openssl/rand.h&gt;
  #include &lt;openssl/ssl.h&gt;
  #include &lt;openssl/x509v3.h&gt;
  
  #include &lt;pthread.h&gt;
  
  #define PORT            "6001"
  #define SERVER          "splat.zork.org"
  #define CLIENT          "shell.zork.org"
  
  void handle_error(const char *file, int lineno, const char *msg)
  {
      fprintf(stderr, "** %s:%i %s\n", file, lineno, msg);
      ERR_print_errors_fp(stderr);
      exit(-1);
  }
  
  void init_OpenSSL(void)
  {
      if (!THREAD_setup() || !SSL_library_init())
      {
          fprintf(stderr, "** OpenSSL initialization failed!\n");
          exit(-1);
      }
      SSL_load_error_strings();
  }
  
  
  void do_server_loop(BIO *conn)
  {
      int  done, err, nread;
      char buf[80];
  
      do
      {
          for (nread = 0;  nread &lt; sizeof(buf);  nread += err)
          {
               err = BIO_read(conn, buf + nread, sizeof(buf) - nread);
               if (err &lt;= 0)
                   break;
          }
          fwrite(buf, 1, nread, stdout);
      }
      while (err &gt; 0);
  }
  
  void *server_thread(void *arg)
  {
      BIO *client = (BIO *)arg;
  
      pthread_detach(pthread_self(  ));
  
      fprintf(stderr, "Connection opened.\n");
      do_server_loop(client);
      fprintf(stderr, "Connection closed.\n");
  
      BIO_free(client);
      ERR_remove_state(0);
  }
  
  int main(int argc, char *argv[])
  {
      BIO *acc, *client;
  
      pthread_t tid;
  
      init_OpenSSL();
  
      acc = BIO_new_accept(PORT);
      if (!acc)
          handle_error(__FILE__, __LINE__, "Error creating server socket");
  
      if (BIO_do_accept(acc) &lt;= 0)
          handle_error(__FILE__, __LINE__, "Error binding server socket");
  
      for (;;)
      {
          if (BIO_do_accept(acc) &lt;= 0)
              handle_error(__FILE__, __LINE__, "Error accepting connection");
  
          client = BIO_pop(acc);
          pthread_create(&amp;tid, NULL, server_thread, client);
      }
  
      BIO_free(acc);
      return 0;
  }
  </code></pre>
  
  <p>I'm compiling the source using gcc:</p>
  
  <pre><code>$ gcc -o server server.c -lssl -crypto -lpthread -lm
  Undefined symbols for architecture i386:
    "_ERR_print_errors_fp", referenced from:
        _handle_error in ccgAll3d.o
    "_THREAD_setup", referenced from:
        _init_OpenSSL in ccgAll3d.o
    "_BIO_read", referenced from:
        _do_server_loop in ccgAll3d.o
    "_BIO_free", referenced from:
        _server_thread in ccgAll3d.o
    "_ERR_remove_state", referenced from:
        _server_thread in ccgAll3d.o
    "_BIO_new_accept", referenced from:
        _main in ccgAll3d.o
    "_BIO_ctrl", referenced from:
        _main in ccgAll3d.o
    "_BIO_pop", referenced from:
        _main in ccgAll3d.o
  ld: symbol(s) not found for architecture i386
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Can anyone tell me why is this happening?</p>
  
  <p>I got something similar some time ago and solved it by defining the functions that were problematic but this doesn't work anymore.</p>
  
  <p>I'm compiling the source on a Mac OS X 10.6.8.
  Is it because I'm using a 32-bit system?</p>
  
  <p>This is the command I'm compiling with <code>gcc -Wall -lssl -crypto -lpthread -lm -o server server.c</code></p>