Question

I am attempting to move my module to Linux Apache 2.4 and I am having linking issues. On windows a libhttpd.lib is available to link against as well as the apr/apr-util libraries. lib* httpd apr and aprutil are all statically linked on my windows installation. I want to do the same for the Linux installation.

According to the limited documentation available I am unable to use APXS because my module is written in C++.

I am having difficulties finding the archive files for the server on Linux. What do I need to link against for my module to work?

The source is able to link and execute on a Windows host.

Sample errors:

    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:367: undefined reference to `pthread_mutexattr_init'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:374: undefined reference to `pthread_mutexattr_setpshared'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:384: undefined reference to `pthread_mutexattr_setrobust_np'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:393: undefined reference to `pthread_mutexattr_setprotocol'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:414: undefined reference to `pthread_mutexattr_destroy'
    /home/ec2-user/httpd-2.4.2/srclib/apr/locks/unix/proc_mutex.c:408: undefined reference to `pthread_mutexattr_destroy'

Thanks

Was it helpful?

Solution

These errors means you didn't add -pthread to your compile command line, so you're not getting the pthread library linked in.

(Note: it's -pthread, not -lpthread - it's not just a linker option.)

OTHER TIPS

For those building Apache modules in C++ and want dynamic linking, here's the g++ command line I used to successfully build a module; briefly tested on Apache 2.2.22/CentOS 6.2.

g++ [my files].cpp  -I/httpd/include/ -I/httpd/srclib/apr/include/ 
    -I/httpd/srclib/apr-util/include/ -I/usr/include/ -I/usr/include/apr-1/ 
    -I/httpd/os/unix/ -shared -fPIC -o mod_mymodule.so

I'm an Apache/linux programming noob and was unable to find this info anywhere else; thanks to the OP's solution I was able to finish the job after a few days of frustration.

Here's also a link which helped explain how to work around the 'unresolved reference' linker issue when it couldn't find the apache functions contained within the httpd server core code (libhttpd.lib on Windows) -- which doesn't exist in *nix, unless you make it manually like the OP did. Basically, the answer was to use the -shared flag so these references are automagically resolved at run time. http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

Don't forget the 'extern C' in your module code, and build in DSO support when building HTTPD.

Hope this helps someone else!

So anyone else searching for this may get the answer.

  1. Download whatever Apache version (higher than 2.2) source from the apache httpd site to the home directory
  2. Unpack
  3. Configure with everything or simply just specify no options (You can reconfigure later for the actual build)
  4. perform a make on the source ( don't do a make install yet! just need the object files)
  5. create a directory that will hold your libraries and source for your own module. Say "foo" at the top level of your home dir (or any you desire)
  6. assuming home dir: execute (find ~/httpdX.X -name '*.o' -exec cp {} ~/foo \;) The command is in parenthesis. This will copy all compiled objects to foo.
  7. execute (find ~/foo -name '*.o' -exec ar r libhttpd.a {} \;) which will create an archive of the code for you.
  8. Then just include these switches in your compile definition for gcc or g++ (-Wl,-Bstatic -lhttpd -lpcre -lpcreposix -lapr-1 -laprutil-1 -Wl,-Bdynamic -pthread -ldl -lcrypt)
  9. Clean up your foo directory if you like by running rm on the *.o files

You do not need to compile statically like I needed to, but I wanted to be able to move my module to any Linux host without worry about the necessary components. Apache needs pcre(regex), apr(all libraries), threads (proc/thread mutex), dl(dynamic loading), and crypt(apr password) to work. Since thread, dl, and crypt will most like already be on the machine, I chose to not compile them statically.

Happy hunting. I hope my never ending story over 3 days helps someone else!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top