"linking a php extension written in c" Code Answer


If your C extension code uses a shared library, you need to declare that in the config.m4 file.

I strongly recommend using the ext_skel script that's included in the PHP source to generate a skeleton config.m4:

./ext_skel --extname=myextension

Since you're linking to a library, by convention you should use the --with-myextension options (as opposed to --enable-myextension). Uncomment the relevant lines in the config.m4 and fill in the details of your lib.

Something like the following:

  # --with-myextension -> check for lib and symbol presence
  LIBNAME=the_lib_your_extension_needs # you may want to change this
  LIBSYMBOL=some_symbol_in_the_lib_you_extension_needs # you most likely want to change this 

    AC_MSG_ERROR([wrong $LIBNAME lib version or lib not found])
    -L$MYEXTENSION_DIR/lib -ldl

Then to build it, run:

./configure --with-myextension

Finally you need to copy your module (or ln -s) to wherever your system expects to find it.

If that all worked then php -m should include your module in the list.

Unfortunately I've never found a good online reference to PHP's config.m4 commands - the books for this are Sara Golemon's Extending and Embedding PHP and also parts of George Schlossnagle's Advanced PHP Programming.

There's a reasonable beginners guide to creating PHP extensions by Sara Goleman here, but for the meat you really need her book.

By etuannv on December 15 2022

Answers related to “linking a php extension written in c”

Only authorized users can answer the search term. Please sign in first, or register a free account.