title: How can I link my C code with the PCRE library? (Linker errors currently being thrown.)
body: <h2>The Problem</h2>
  <p><em><strong>Note:</strong></em> I originally had this problem in a much larger project; so I pared the code down to the test case you see below.</p>
  <p>I cannot figure out how to get the following test code to compile. Specifically, it appears as if the linker cannot find the PCRE library (see below for how PCRE was configured).  And this despite the explicit <code>-L/usr/local/lib -lpcre</code> being passed to the linker (PCRE is installed in the <code>/usr/local</code> directory structure).</p>
  <p>What am I doing wrong? :-(</p>
  <p>The console output is:</p>
  <pre><code>$ make
  rm -f ./*.o
  gcc -ansi -Wall -pedantic-errors -I/usr/local/include -g0 -O3 -static -static-libgcc -march=i686 -malign-double -m128bit-long-double -c main.c -o main.o
  gcc -ansi -Wall -pedantic-errors -L/usr/local/lib -lpcre -g0 -O3 -static -static-libgcc -march=i686 -malign-double -m128bit-long-double main.o -o pcre_test_1_i686
  main.o:main.c:(.text+0x100): undefined reference to `pcre_compile2'
  main.o:main.c:(.text+0x12e): undefined reference to `pcre_study'
  main.o:main.c:(.text+0x16e): undefined reference to `pcre_exec'
  main.o:main.c:(.text+0x19f): undefined reference to `pcre_copy_substring'
  collect2: ld returned 1 exit status
  make: *** [all] Error 1
  </code></pre>
  <hr />
  <h2>Relevant Files</h2>
  <hr />
  <p><strong>PCRE Configuration and Compile Environment</strong></p>
  <pre><code>./configure --disable-shared --enable-static --disable-cpp --enable-rebuild-chartables --enable-utf8 --enable-unicode-properties --enable-newline-is-any --disable-stack-for-recursion --with-posix-malloc-threshold=2 --with-link-size=4
  
  CC=&quot;gcc&quot;
  CFLAGS=&quot;-g0 -O3 -static -static-libgcc -march=i686 -malign-double -m128bit-long-double&quot;
  LD_RUN_PATH=&quot;/usr/local/include:/usr/local/lib&quot;
  </code></pre>
  <hr />
  <p><strong>main.c</strong></p>
  <pre><code>#define PCRE_STATIC  
  #include &lt;pcre.h&gt;  
  #include &lt;stdlib.h&gt;  
  #include &lt;errno.h&gt;  
  #include &lt;stdio.h&gt;  
  #include &lt;string.h&gt;  
  
  #define OUTPUT_SIZE 12
  #define SUBSTRING_SIZE 16
  
  int main(int argc, char *argv[]) {
  
    pcre *re;
    pcre_extra *extra;
    const char *input = &quot;get food&quot;;
    const char *pattern = &quot;^\\s*get\\s+(\\w+)\\s*$\0&quot;;
    int options = PCRE_CASELESS | PCRE_UTF8 | PCRE_UCP;
    int error_code = 0;
    int error_offset = 0;
    const char *compile_error;
    const char *study_error;
    int output[OUTPUT_SIZE];
    char substring[SUBSTRING_SIZE];
    int matched = 0;
    int is_error = 0;
    int index = 0;
    for(index = 0; index &lt; OUTPUT_SIZE; index++) {
      output[index] = 0;
    }
    re = pcre_compile2( pattern,
                        options,
                        &amp;error_code,
                        &amp;compile_error,
                        &amp;error_offset,
                        NULL );
    if(re == NULL) {
      fprintf(stderr, &quot;PCRE regular expression error at position %d: %s&quot;, error_offset, compile_error);
      exit(EXIT_FAILURE);
    }
    if(error_code == 0) {
      extra = pcre_study( re,
                          0,
                          &amp;study_error );
    }
    else {
      fprintf(stderr, &quot;PCRE regular expression error at position %d: %s&quot;, error_offset, compile_error);
      extra = NULL;
    }
  
    matched = pcre_exec( re,
                         extra,
                         input,
                         (int)strlen(input),
                         0, /* Start at the beginning of the string */
                         0,
                         output,
                         OUTPUT_SIZE );
    if(matched &gt; 1) {
      int status = pcre_copy_substring( input,
                                        output,
                                        matched,
                                        1,
                                        substring,
                                        SUBSTRING_SIZE );
      if(status &lt; 0) {
        switch(status) {
          case PCRE_ERROR_NOMEMORY:
            fprintf(stderr, &quot;PCRE substring extraction error: %s&quot;, &quot;Buffer too small&quot;);
            break;
          case PCRE_ERROR_NOSUBSTRING:
            fprintf(stderr, &quot;PCRE substring extraction error: %s&quot;, &quot;Invalid substring number&quot;);
            break;
        }
        is_error = 1;
      }
    }
  
    printf(&quot;Capture group 1 is: '%s'\n&quot;, substring);
    if(is_error) {
      printf(&quot;There was an error with the pcre_copy_substring() function.\n&quot;);
    }
  
    return EXIT_SUCCESS;
  }
  </code></pre>
  <hr />
  <p><strong>Makefile</strong></p>
  <pre><code>PACKAGE = pcre_test
  VERSION = 1
  
  # -ansi == ANSI C
  # -std=iso9899:199409 == ANSI C w/ Amendment 1
  # -std=c99 == ISO C99
  GCC_CMD = gcc -ansi -Wall -pedantic-errors
  
  # Generic: i686
  # Intel: core2, corei7, corei7-avx
  # AMD: k8-sse3, opteron-sse3, athlon64-sse3, amdfam10
  ARCH = i686
  
  RELEASE_FILE = $(PACKAGE)_$(VERSION)_$(ARCH)
  
  GCC_OPTIONS = -g0 -O3 -static -static-libgcc -march=$(ARCH) -malign-double -m128bit-long-double
  GCC_COMPILE = $(GCC_CMD) -I/usr/local/include $(GCC_OPTIONS)
  GCC_LINK = $(GCC_CMD) -L/usr/local/lib -lpcre $(GCC_OPTIONS)
  
  GCC_LINK_ALL = $(GCC_LINK) main.o
  
  all: clean build_main
          $(GCC_LINK_ALL) -o $(RELEASE_FILE)
  
  build_main:
          $(GCC_COMPILE) -c main.c -o main.o
  
  clean:
          rm -f ./*.o
  </code></pre>
  <hr />
  <p><em><strong>[EDIT]</strong></em></p>
  <h2>The Answer Has Been Found!</h2>
  <p>All external library flags must be listed <em>after</em> all of the object files.  Thus, the <strong>Makefile</strong> should have looked like this:</p>
  <pre><code>GCC_LINK = $(GCC_CMD) -L/usr/local/lib $(GCC_OPTIONS)
  
  GCC_LINK_ALL = $(GCC_LINK) main.o -lpcre
  </code></pre>
  <p>Thank you to everyone who responded with answers.  :-)</p>