title: How can I write an Apache module in C++?
body: <p>I'd like to write an Apache module in C++. I tried a very barebones module to start:</p>
  
  <pre><code>#include "httpd.h"
  #include "http_core.h"
  #include "http_protocol.h"
  #include "http_request.h"
  
  static void register_hooks(apr_pool_t *pool);
  static int example_handler(request_rec *r);
  
  extern "C" module example_module;
  
  module AP_MODULE_DECLARE_DATA example_module = {
      STANDARD20_MODULE_STUFF, NULL, NULL, NULL, NULL, NULL, register_hooks
  };
  
  static void register_hooks(apr_pool_t *pool) {
      ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
  }
  
  static int example_handler(request_rec *r) {
      if (!r-&gt;handler || strcmp(r-&gt;handler, "example"))
          return (DECLINED);
  
      ap_set_content_type(r, "text/plain");
      ap_rputs("Hello, world!", r);
      return OK;
  }
  </code></pre>
  
  <p>Compiling with <code>apxs</code> seems to work just fine, using:</p>
  
  <pre><code>apxs -i -n example_module -c mod_example.cpp
  </code></pre>
  
  <p>However, when I try to start httpd, I get an error. I've inserted some newlines to make it more legible.</p>
  
  <pre><code>httpd: Syntax error on line 56 of /etc/httpd/conf/httpd.conf:
         Syntax error on line 1 of /etc/httpd/conf.modules.d/20-mod_example.conf:
         Can't locate API module structure `example_module' in file /etc/httpd/modules/mod_example.so:
         /etc/httpd/modules/mod_example.so: undefined symbol: example_module
  </code></pre>
  
  <p>Indeed, I can confirm with <code>objdump -t</code> that there is no symbol named <code>example_module</code> in <code>mod_example.so</code>. I find this especially confusing because if I manually compile with</p>
  
  <pre><code>gcc -shared -fPIC -DPIC -o mod_example.so `pkg-config --cflags apr-1` -I/usr/include/httpd mod_example.cpp
  </code></pre>
  
  <p>(which mimics the command I see <code>libtool</code> running from inside <code>apxs</code>), then <code>objdump -t</code> does indeed show an <code>example_module</code> symbol in <code>mod_example.so</code>.</p>
  
  <p>What gives? Why doesn't <code>example_module</code> appear in my <code>.so</code>? What can I do to fix it?</p>