title: postgresql c-extension loads another external library
body: <p>I need to compare pHashes (phash.org) with a hamming distance function.</p>
  
  <p>I tried the one from pg_similarity, but it doesn't seem to work right. (identical pHashes don't have a hamming distance of 0).</p>
  
  <p>So I figured I'd just use a c-extension to use the <code>ph_hamming_distance</code> function that's part of the pHash library.</p>
  
  <p>What I've got:
  phash.c</p>
  
  <pre><code>#include &lt;postgres.h&gt;
  #include &lt;pHash.h&gt;
  #include &lt;fmgr.h&gt;
  #include &lt;utils/bytea.h&gt;
  #include &lt;utils/datum.h&gt;
  
  #ifdef PG_MODULE_MAGIC
  PG_MODULE_MAGIC;
  #endif
  
  PG_FUNCTION_INFO_V1(phash_hamming);
  Datum phash_hamming(PG_FUNCTION_ARGS) {
  
      bytea *bytea1 = PG_GETARG_BYTEA_P(0);
      bytea *bytea2 = PG_GETARG_BYTEA_P(1);
      //FIXME - length of bytea1 &amp; bytea2 must be 4 bytes (64bits)
  
      ulong64 long1 = *((ulong64*) bytea1);
      ulong64 long2 = *((ulong64*) bytea2);
  
      int32 ret = ph_hamming_distance(long1, long2);
  
      PG_RETURN_INT32(ret);
  }
  </code></pre>
  
  <p>Makefile</p>
  
  <pre><code>CXXFLAGS=-I/usr/include/postgresql/server
  LDFLAGS=-Bstatic -lpHash
  all: phash.o
  
  phash.o:
      $(CXX) $(CXXFLAGS) -fpic -c phash.c
      $(CXX) $(LDFLAGS) -shared -o phash.so phash.o
  
  install:
      cp phash.so `pg_config --pkglibdir`
  
  clean:
      rm -f phash.o phash.so
  </code></pre>
  
  <p>SQL</p>
  
  <pre><code> CREATE FUNCTION phash_hamming (bytea1 bytea, bytea2 bytea) RETURNS int AS '$libdir/phash' LANGUAGE C;
  </code></pre>
  
  <p>Error that I'm getting:</p>
  
  <pre><code>ERROR:  could not load library "/usr/lib/postgresql/phash.so": /usr/lib/postgresql/phash.so: undefined symbol: _Z16pg_detoast_datumP7varlena
  </code></pre>
  
  <p>I must not be linking right to postgresql somehow?</p>