链接用 C 编写的 PHP 扩展

发布于 2024-08-06 15:40:27 字数 69 浏览 8 评论 0原文

编辑:修改我的问题

当用 C 构建外部 PHP 模块时,如何链接共享对象?

Edit: Revising My Question

When building an external PHP module in C, how do I link shared objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-08-13 15:40:27

如果您的 C 扩展代码使用共享库,则需要在 config.m4 文件中进行声明。

强烈建议使用ext_skel PHP 源代码中包含的用于生成框架 config.m4 的脚本:

./ext_skel --extname=myextension

由于您要链接到库,按照惯例,您应该使用 -- with-myextension 选项(与 --enable-myextension 相反)。取消注释 config.m4 中的相关行并填写您的库的详细信息。

如下所示:

  # --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 

  PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
  [
    PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MYEXTENSION_DIR/lib, MYEXTENSION_SHARED_LIBADD)
    AC_DEFINE(HAVE_MYEXTENSIONLIB,1,[ ])
  ],[
    AC_MSG_ERROR([wrong $LIBNAME lib version or lib not found])
  ],[
    -L$MYEXTENSION_DIR/lib -ldl
  ])

然后要构建它,请运行:

phpize
./configure --with-myextension
make

最后,您需要将模块(或 ln -s )复制到系统期望找到它的任何位置。

如果一切正常,那么 php -m 应该将您的模块包含在列表中。

不幸的是,我从来没有找到关于 PHP config.m4 命令的良好在线参考 - 这方面的书籍是 Sara Golemon 的扩展和嵌入 PHP 以及 George Schlossnagle 的高级 PHP 的一部分编程

Sara Goleman 有一个合理的初学者创建 PHP 扩展指南 这里,但是对于肉,你真的需要她的书。

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 

  PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
  [
    PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MYEXTENSION_DIR/lib, MYEXTENSION_SHARED_LIBADD)
    AC_DEFINE(HAVE_MYEXTENSIONLIB,1,[ ])
  ],[
    AC_MSG_ERROR([wrong $LIBNAME lib version or lib not found])
  ],[
    -L$MYEXTENSION_DIR/lib -ldl
  ])

Then to build it, run:

phpize
./configure --with-myextension
make

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文