title: Problems linking header files with Rust bindgen
body: <p>According to the Rust bindgen tutorial at <a href="https://rust-lang.github.io/rust-bindgen/tutorial-3.html" rel="nofollow noreferrer">https://rust-lang.github.io/rust-bindgen/tutorial-3.html</a>, I am trying to generate the FFI for existing C header files. However, I am having trouble linking the header files.</p>
  <p>The repository at <a href="https://github.com/studersi/rust-test-apache" rel="nofollow noreferrer">https://github.com/studersi/rust-test-apache</a> holds all the files necessary to reproduce the issue including a README file containing the necessary commands. The most relevant files are listed at the end of the question.</p>
  <p>I am using a Docker container to build the project (see Dockerfile below). The C header files are downloaded from the Internet and placed in the subdirectory <code>downloads/</code>.</p>
  <p>The includes for the linker (see build.rs below) are based on the includes the <code>apxs</code> tool uses to build a similar C example according to the <a href="https://httpd.apache.org/docs/2.4/developer/modguide.html" rel="nofollow noreferrer">Apache httpd module development guide</a> on which my project is based.</p>
  <pre><code>$ apxs -a -c mod_example.c
  /usr/share/apr-1.0/build/libtool  --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -prefer-pic -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security  -Wdate-time -D_FORTIFY_SOURCE=2   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -pthread  -I/usr/include/apache2  -I/usr/include/apr-1.0   -I/usr/include/apr-1.0 -I/usr/include  -c -o mod_example.lo mod_example.c &amp;&amp; touch mod_example.slo
  libtool: compile:  x86_64-linux-gnu-gcc -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/apache2 -I/usr/include/apr-1.0 -I/usr/include/apr-1.0 -I/usr/include -c mod_example.c  -fPIC -DPIC -o .libs/mod_example.o
  /usr/share/apr-1.0/build/libtool  --mode=link --tag=disable-static x86_64-linux-gnu-gcc -Wl,--as-needed -Wl,-z,relro -Wl,-z,now    -o mod_example.la  -rpath /usr/lib/apache2/modules -module -avoid-version    mod_example.lo
  libtool: link: x86_64-linux-gnu-gcc -shared  -fPIC -DPIC  .libs/mod_example.o    -Wl,--as-needed -Wl,-z -Wl,relro -Wl,-z -Wl,now   -Wl,-soname -Wl,mod_example.so -o .libs/mod_example.so
  libtool: link: ( cd &quot;.libs&quot; &amp;&amp; rm -f &quot;mod_example.la&quot; &amp;&amp; ln -s &quot;../mod_example.la&quot; &quot;mod_example.la&quot; )
  
  </code></pre>
  <p>After starting the container and building the Cargo project, I get errors that the header files to be converted cannot be found.</p>
  <pre><code>$ cargo build
     Compiling mod_rs v0.1.0 (/home/rustacian/mod_rs)
  error: failed to run custom build command for `mod_rs v0.1.0 (/home/rustacian/mod_rs)`
  
  Caused by:
    process didn't exit successfully: `/home/rustacian/mod_rs/target/debug/build/mod_rs-a68981041b3e3908/build-script-build` (exit code: 101)
    --- stdout
    cargo:include=/home/rustacian/downloads/httpd-2.4.46/include
    cargo:include=/home/rustacian/downloads/apr-1.7.0/include
    cargo:include=/usr/include/apache2
    cargo:include=/usr/include/apr-1.0
    cargo:rerun-if-changed=wrapper.h
  
    --- stderr
    wrapper.h:2:10: fatal error: 'apr_hash.h' file not found
    wrapper.h:2:10: fatal error: 'apr_hash.h' file not found, err: true
    thread 'main' panicked at 'Unable to generate bindings: ()', build.rs:34:10
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  </code></pre>
  <p>Note that I already tried including the header files using different commands (<code>cargo:include</code> and <code>.clang_arg</code>) and from different locations (downloaded manually and installed using system packages) but nothing has worked so far.</p>
  <pre><code>[...]
      println!(&quot;cargo:include=/home/rustacian/downloads/httpd-2.4.46/include&quot;);
      println!(&quot;cargo:include=/home/rustacian/downloads/apr-1.7.0/include&quot;);
      println!(&quot;cargo:include=/usr/include/apache2&quot;);
      println!(&quot;cargo:include=/usr/include/apr-1.0&quot;);
  [...]
          .clang_arg(&quot;-I/usr/include/apache2 -I/usr/include/apr-1.0 -I/home/rustacian/downloads/httpd-2.4.46/include -I/home/rustacian/downloads/apr-1.7.0/include&quot;)
  [...]
  </code></pre>
  <p><em>Why is bindgen unable to find the header files? Any help would be appreciated.</em></p>
  <hr />
  <p><strong>Dockerfile</strong></p>
  <pre><code>FROM rust
  
  ARG USERNAME=&quot;rustacian&quot;
  
  RUN apt-get update
  RUN apt-get -y install --no-install-recommends apache2-dev llvm librust-bindgen-dev
  
  RUN groupadd -r ${USERNAME} &amp;&amp; useradd -s /bin/bash -g ${USERNAME} ${USERNAME}
  USER ${USERNAME}
  
  WORKDIR /home/${USERNAME}
  </code></pre>
  <p><strong>build.rs</strong></p>
  <pre class="lang-rust prettyprint-override"><code>extern crate bindgen;
  
  use std::env;
  use std::path::PathBuf;
  
  fn main() {
      // Tell cargo to tell rustc to link the
      // shared library.
      println!(&quot;cargo:include=/home/rustacian/downloads/httpd-2.4.46/include&quot;);
      println!(&quot;cargo:include=/home/rustacian/downloads/apr-1.7.0/include&quot;);
      println!(&quot;cargo:include=/usr/include/apache2&quot;);
      println!(&quot;cargo:include=/usr/include/apr-1.0&quot;);
  
      // Tell cargo to invalidate the built crate whenever the wrapper changes
      println!(&quot;cargo:rerun-if-changed=wrapper.h&quot;);
  
      // The bindgen::Builder is the main entry point
      // to bindgen, and lets you build up options for
      // the resulting bindings.
      let bindings = bindgen::Builder::default()
          // The input header we would like to generate
          // bindings for.
          .header(&quot;wrapper.h&quot;)
          // Add the includes for C header files.
          // Derived from https://httpd.apache.org/docs/2.4/developer/modguide.html
          // $ apxs -a -c mod_example.c
          .clang_arg(&quot;-I/usr/include/apache2 -I/usr/include/apr-1.0 -I/home/rustacian/downloads/httpd-2.4.46/include -I/home/rustacian/downloads/apr-1.7.0/include&quot;)
          // Tell cargo to invalidate the built crate whenever any of the
          // included header files changed.
          .parse_callbacks(Box::new(bindgen::CargoCallbacks))
          // Finish the builder and generate the bindings.
          .generate()
          // Unwrap the Result and panic on failure.
          .expect(&quot;Unable to generate bindings&quot;);
  
      // Write the bindings to the $OUT_DIR/bindings.rs file.
      let out_path = PathBuf::from(env::var(&quot;OUT_DIR&quot;).unwrap());
      bindings
          .write_to_file(out_path.join(&quot;bindings.rs&quot;))
          .expect(&quot;Couldn't write bindings!&quot;);
  }
  </code></pre>
  <p><strong>wrapper.h</strong></p>
  <pre><code>#include &quot;stdio.h&quot;
  #include &quot;apr_hash.h&quot;
  #include &quot;ap_config.h&quot;
  #include &quot;ap_provider.h&quot;
  #include &quot;httpd.h&quot;
  #include &quot;http_core.h&quot;
  #include &quot;http_config.h&quot;
  #include &quot;http_log.h&quot;
  #include &quot;http_protocol.h&quot;
  #include &quot;http_request.h&quot;
  </code></pre>
  <p><strong>Cargo.toml</strong></p>
  <pre><code>[package]
  name = &quot;mod_rs&quot;
  version = &quot;0.1.0&quot;
  authors = [&quot;studersi &lt;*****@******.***&gt;&quot;]
  edition = &quot;2018&quot;
  
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
  
  [dependencies]
  libc = &quot;*&quot;
  
  [build-dependencies]
  bindgen = &quot;*&quot;
  </code></pre>