C-language php HelloWorld extension

Source: Internet
Author: User
Tags install php php source code zend egrep

1. Download PHP source code

wget HTTP://CN2.PHP.NET/DISTRIBUTIONS/PHP-5.6.10.TAR.GZTAR-ZXVF php-5.6.10.tar.gz

2. establish an extended development framework ./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&NBSP, .... 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.

3, enter the root directory of PHP source code, edit the file vim ext/helloworld/config.m4

Get rid of the DNL in front of these lines of code
Php_arg_enable (HelloWorld, whether to ENABLE HelloWorld support,make sure so the comment is aligned:[--enable-hellowor LD Enable HelloWorld support])

4 . Execute the command in the PHP source root directory./buildconf--force

Forcing buildconfremoving Configure cachesbuildconf:checking installation...buildconf:autoconf version 2.69 (OK) Rebuilding configurerebuilding main/php_config.h.in

5, PHP source code in the root directory compiled PHP program, note the command for./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 ... The last error that appears, does not deal with Configure: error: xml2-config not found. please check your  libxml2 installation.

6,. Enter our extension directory HelloWorld, execute command phpize (install phpize via sudo apt-get install Php5-dev), at which point your extension directory generates many files that can be used for post-compilation.

CD php-5.6.10/ext/helloworldphpizeconfiguring for:php API version:20121113zend Module API No:20121212zend E XTension Api no:220121212

7 . Compile our extension in the HelloWorld directory./configure--with-php-config=/usr/bin/php-config (php-config using your own environment)-- Enable-helloworld

You can find the location of your php-config file via a command (Find/-name Php-config) as follows: My address is/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 ...

8, Enter the extension HelloWorld directory, edit the file php_helloworld.h, add the function PHP_function (helloworldtest) in the last line;

Php_function (HelloWorldTest);

9. Open HELLOWORD.C with vim, implement our function in Helloworld.c, then add HelloWorldTest function to helloworld_functions[], save exit

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[] */}; 

10, execute make command make compile extension, I run the process is still relatively smooth. If there is an error, please take a serious look at the previous step is wrong, I do the first time also wrong is generally the previous steps have a problem.

/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 '.

11. Install PHP

Apt-get Install PHP5 php5-gd php5-cli

11. Copy the compiled helloworld.so file to your native PHP extension directory

Created: info.php content: Phpinfo ();p hp 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. [Email protected]:/php/php-5.6.10/ext/helloworld# CP modules/helloworld.so/usr/lib/php5/20121212/

12. Configure PHP.ini to open helloworld.so extension

PHP info.php |  grep php.iniloaded Configuration File =/etc/php5/cli/php.ini[email protected]:/php# vim/etc/php5/cli/conf.d/ Ini

13. Create echo.php

<?php Print_r (HelloWorldTest ()); Result: Hello world!

14. Problems encountered in Docker

1. Apt-get install AUTOCONF2. Installation of dependent packages such as GCC make









C-language php HelloWorld extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.