There are two stages to building the shared object. First all the sourec files must be compiled to object files. These object files must be compiled with Position Independent Code. Second, the objects and external libraries must be linked into a shared object.
If your module has any part written in C++ then you must use
the C++ compiler to do your linking, otherwise you can use
ld (or have
apxs automatically call
ld for you).
When you link you must specify to the linker that you are building a shared object file and not an executable. This is to prevent the creation of the startup code and to allow the linking to be successful even if some global symbols are unresolved (these will be resolved by symbols from the Apache runtime that loads the module).
In g++ the flag "
-shared" is used to specify that
a shared object file should be produced.
APXS=apxs APXS_LIBS_SHLIB=`$(APXS) -q LIBS_SHLIB` #link mod_cpphello.so: mod_cpphello.o g++ -fPIC -shared -o $@ $< $(APXS_LIBS_SHLIB)