title: Ambiguous C binding name in Fortran
body: <p>I'm trying to build and use a library from <a href="https://github.com/SCM-NV/ftl" rel="nofollow noreferrer">this</a> repo (FTL). FTL provides Fortran wrappers around POSIX regular expressions and can be built (according to its <a href="https://github.com/SCM-NV/ftl/wiki/ftlRegex" rel="nofollow noreferrer">wiki</a>) either using standard C library implementation from <code>&lt;regex.h&gt;</code> or PCRE implementation from <code>&lt;pcreposix.h&gt;</code>. I was able to successfully compile FTL's unit tests with <code>make test</code>, but their execution fails at runtime.</p>
  <p>Here's what I think happens. The two implementations have different values of certain constants. These values are defined by execution of a C program (<code>configure/configure_ftlRegex.c</code>), where either <code>&lt;pcreposix.h&gt;</code> or <code>&lt;regex.h&gt;</code> is included, depending on macro definitions during compilation. By default (and this is how I compiled the unit tests) these constants are generated using the implementation from <code>&lt;pcreposix.h&gt;</code>.</p>
  <p>Now, the following binding to a C function with name <code>regexec</code> is defined in <code>src/ftlRegex.F90</code>:</p>
  <pre><code>function C_regexec(preg, string, nmatch, pmatch, eflags) result(status) bind(C,name='regexec')
     import
     type(C_ptr)           , intent(in) , value :: preg
     character(kind=C_char), intent(in)         :: string(*)
     integer(C_size_t)     , intent(in) , value :: nmatch
     type(C_regmatch_t)    , intent(out)        :: pmatch(*)
     integer(C_int)        , intent(in) , value :: eflags
     integer(C_int)                             :: status
  end function
  </code></pre>
  <p>However, there are two implementations of <code>regexec</code> available to the linker (with identical signatures): one from the standard C library (linked automatically), and another from <code>libpcreposix</code> (linked via <code>-lpcre -lpcreposix</code>). Which one does it actually bind to?</p>
  <p>Executing <code>ldd tests</code> shows the following output:</p>
  <pre><code>linux-vdso.so.1 (0x00007ffd255a0000)
  libgfortran.so.5 =&gt; /usr/lib/x86_64-linux-gnu/libgfortran.so.5 (0x00007f721a27c000)
  libm.so.6 =&gt; /usr/lib/x86_64-linux-gnu/libm.so.6 (0x00007f721a12d000)
  libc.so.6 =&gt; /usr/lib/x86_64-linux-gnu/libc.so.6 (0x00007f7219f3b000)
  libquadmath.so.0 =&gt; /usr/lib/x86_64-linux-gnu/libquadmath.so.0 (0x00007f7219ef1000)
  libgcc_s.so.1 =&gt; /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7219ed6000)
  /lib64/ld-linux-x86-64.so.2 (0x00007f721a647000)
  </code></pre>
  <p>As you can see, <code>libpcre</code> and <code>libpcreposix</code> are not on the list, which makes me believe that the linker chose to bind to the <code>&lt;regex.h&gt;</code> implementation, which is wrong because constants from <code>&lt;pcreposix.h&gt;</code> were used during the compilation. How does linker decide what implementation should be used in cases like this? Is there a way to enforce usage of particular implementation or at least check which implementation is used?</p>
  <p>Note: The following command was used to link the unit tests (generated by makefile):</p>
  <pre><code>gfortran -std=f2008 -fall-intrinsics -ffree-line-length-none -Wall -Wextra -Wpedantic -Wno-target-lifetime -Wno-compare-reals -Jbuild.gnu.debug -g -Og -fcheck=bounds,do,mem,pointer,recursion -Isrc -Itests  -DUSE_PCRE tests/tests.F90 build.gnu.debug/*.o -lpcreposix -lpcre -o build.gnu.debug/tests
  </code></pre>
  <p>executed with GNU Fortran (Ubuntu 9.3.0-10ubuntu2) 9.3.0.</p>