Home Backend Development PHP Tutorial C语言PHP的helloworld扩展

C语言PHP的helloworld扩展

Jun 23, 2016 pm 01:31 PM

1、下载php源码

wget http://cn2.php.net/distributions/php-5.6.10.tar.gztar -zxvf php-5.6.10.tar.gz
Copy after login

2、建立扩展开发框架 ./ext_skel --extname=helloworld

cd php-5.6.10/ext/./ext_skel --extname=helloworldCreating directory helloworldCreating basic files: config.m4 config.w32 .gitignore helloworld.c php_helloworld.h CREDITS EXPERIMENTAL tests/001.phpt helloworld.php [done].To use your new extension, you will have to execute the following steps:1.  $ cd ..2.  $ vi ext/helloworld/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-helloworld5.  $ make6.  $ ./sapi/cli/php -f ext/helloworld/helloworld.php7.  $ vi ext/helloworld/helloworld.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/helloworld/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.
Copy after login

3、进入php源码的根目录, 编辑文件 vim ext/helloworld/config.m4

去掉这几行代码前面的dnl
Copy after login
PHP_ARG_ENABLE(helloworld, whether to enable helloworld support,Make sure that the comment is aligned:[  --enable-helloworld           Enable helloworld support])
Copy after login

4、在php源码根目录执行命令 ./buildconf --force

Forcing buildconfRemoving configure cachesbuildconf: checking installation...buildconf: autoconf version 2.69 (ok)rebuilding configurerebuilding main/php_config.h.in
Copy after login

5、在php源码的根目录编译php程序,注意命令为 ./configure --with-helloworld

configure: WARNING: unrecognized options: --with-helloworldchecking for grep that handles long lines and -e... /bin/grepchecking for egrep... /bin/grep -Echecking for a sed that does not truncate output... /bin/sedchecking build system type... x86_64-unknown-linux-gnuchecking host system type... x86_64-unknown-linux-gnuchecking target system type... x86_64-unknown-linux-gnuchecking for cc... ccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables... checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether cc accepts -g... yeschecking for cc option to accept ISO C89... none neededchecking how to run the C preprocessor... cc -Echecking for icc... nochecking for suncc... no......最后出现的error,没有处理configure: error: xml2-config not found. Please check your libxml2 installation.
Copy after login

6、. 进入我们的扩展目录helloworld,执行命令 phpize(通过 sudo apt-get install php5-dev 安装 phpize),此时你的扩展目录会生成很多文件,可以用于后期编译。

cd php-5.6.10/ext/helloworldphpizeConfiguring for:PHP Api Version:         20121113Zend Module Api No:      20121212Zend Extension Api No:   220121212
Copy after login

7、在helloworld目录编译我们的扩展 ./configure --with-php-config=/usr/bin/php-config(使用你自己环境的php-config) --enable-helloworld

你可以通过一条命令查找你的php-config文件的位置(find / -name php-config)如下:我的地址为/usr/bin/php-config

./configure --with-php-config=/usr/bin/php-configchecking for grep that handles long lines and -e... /bin/grepchecking for egrep... /bin/grep -Echecking for a sed that does not truncate output... /bin/sedchecking for cc... ccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables... checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether cc accepts -g... yeschecking for cc option to accept ISO C89... none neededchecking how to run the C preprocessor... cc -Echecking for icc... nochecking for suncc... nochecking whether cc understands -c and -o together... yeschecking for system library directory... libchecking if compiler supports -R... no......
Copy after login

8、进入扩展helloworld目录,编辑文件php_helloworld.h,在最后一行添加函数PHP_FUNCTION(helloworldTest);

PHP_FUNCTION(helloworldTest);
Copy after login

9、用vim 打开helloword.c,在helloworld.c中实现我们的函数,之后 将helloworldTest函数加入到helloworld_functions[]中,保存退出

PHP_FUNCTION(helloworldTest){        RETURN_STRING("Hello World !", 1);}const zend_function_entry helloworld_functions[] = {        PHP_FE(confirm_helloworld_compiled,     NULL)           /* For testing, remove later. */        PHP_FE(helloworldTest, NULL)        PHP_FE_END      /* Must be the last line in helloworld_functions[] */};
Copy after login

10、执行make命令 make 编译扩展,我在运行的过程中还是比较顺利的。如果出现错误,请认真看前面步骤是否有错,我在第一遍做的时候也有错一般都是前面步骤有问题。

/bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=compile cc  -I. -I/php/php-5.6.10/ext/helloworld -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /php/php-5.6.10/ext/helloworld/helloworld.c -o helloworld.lo libtool: compile:  cc -I. -I/php/php-5.6.10/ext/helloworld -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /php/php-5.6.10/ext/helloworld/helloworld.c  -fPIC -DPIC -o .libs/helloworld.o/bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=link cc -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -o helloworld.la -export-dynamic -avoid-version -prefer-pic -module -rpath /php/php-5.6.10/ext/helloworld/modules  helloworld.lo libtool: link: cc -shared  -fPIC -DPIC  .libs/helloworld.o    -O2   -Wl,-soname -Wl,helloworld.so -o .libs/helloworld.solibtool: link: ( cd ".libs" && rm -f "helloworld.la" && ln -s "../helloworld.la" "helloworld.la" )/bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=install cp ./helloworld.la /php/php-5.6.10/ext/helloworld/moduleslibtool: install: cp ./.libs/helloworld.so /php/php-5.6.10/ext/helloworld/modules/helloworld.solibtool: install: cp ./.libs/helloworld.lai /php/php-5.6.10/ext/helloworld/modules/helloworld.lalibtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/sbin" ldconfig -n /php/php-5.6.10/ext/helloworld/modules----------------------------------------------------------------------Libraries have been installed in:   /php/php-5.6.10/ext/helloworld/modulesIf you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify the full pathname of the library, or use the `-LLIBDIR'flag during linking and do at least one of the following:   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable     during execution   - add LIBDIR to the `LD_RUN_PATH' environment variable     during linking   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag   - have your system administrator add LIBDIR to `/etc/ld.so.conf'See any operating system documentation about shared libraries formore information, such as the ld(1) and ld.so(8) manual pages.----------------------------------------------------------------------Build complete.Don't forget to run 'make test'.
Copy after login

11、安装php

apt-get install php5 php5-gd php5-cli
Copy after login

11、将编译生成的helloworld.so 文件复制到你本机的php扩展目录

创建:info.php内容:phpinfo();php info.php | grep extenextension_dir => /usr/lib/php5/20121212 => /usr/lib/php5/20121212mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1.root@4ccdc77255ea:/php/php-5.6.10/ext/helloworld# cp modules/helloworld.so /usr/lib/php5/20121212/
Copy after login

12、配置php.ini 开启 helloworld.so 扩展

php info.php | grep php.iniLoaded Configuration File => /etc/php5/cli/php.iniroot@4ccdc77255ea:/php# vim /etc/php5/cli/conf.d/  php.ini
Copy after login

13、创建echo.php

<?php    print_r(helloworldTest());    结果:Hello World !
Copy after login

14、Docker中遇到的问题

1. apt-get install autoconf2. gcc make等依赖包安装
Copy after login









Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles