C-language php HelloWorld extension

Source: Internet
Author: User
Tags 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 and you'll 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 be satisfied with EXT/HELLOWORLD/CONFIG.M4 Andstep 6 confirms this your module is Compil Ed into PHP. Then, start Writingcode and repeat the last of the 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-hell  Oworld           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 a Nd-e/bin/grepchecking for Egrep .../bin/grep-echecking for a sed to does not truncate output .../bin/sedchecking b Uild 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 is cross compiling ... nochecking for suffix of object files ... ochecking whether W  E is 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 ... nochecking for SUNCC ... no ... The last error that appears, does not handle 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 Extension Api No:   220121212


7. Compile our extension in the HelloWorld directory./configure--with-php-config=/usr/bin/php-config (php-config with 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 fo R egrep .../bin/grep-echecking for a sed to 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 is cross compiling ... nochecking for suffix of object files ... ochecking whether W  E is 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 ... nochecking for SUNCC ... nochecking whether cc UN Derstands-c and-o together ... yeschecking for System library directory ... libchecking if compiler supports-r ..... .


8, enter the extension HelloWorld directory, edit the file php_helloworld.h, add 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 is 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-pi C-module-rpath/php/php-5.6.10/ext/helloworld/modules Helloworld.lo libtool:link:cc-shared-fpic-dpic. Libs/hello World.o-o2-wl,-soname-wl,helloworld.so-o. Libs/helloworld.solibtool:link: (CD ". Libs" && rm-f "Hellowo Rld.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 has been installed in:/ Php/php-5.6.10/ext/helloworld/modulesif ever happen to want to link against installed Librariesin a given directory, L Ibdir, 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 -The Your system administrator add Libdir to '/etc/ld.so.conf ' see all operating system documentation about shared LIBR Aries Formore information, such as the LD (1) and ld.so (8) Manual Pages.----------------------------------------------------------------------Build complete. Don ' tForget 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. root@4ccdc77255ea:/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.iniroot@4ccdc77255ea:/php# vim/etc/php5/cli/conf.d/  php.ini


13. Create echo.php


!--? php print_r (HelloWorldTest ());  Result: Hello world!

14. Problems encountered in Docker

1 apt-get install AUTOCONF2. GCC make and other dependent packages installation 
  • 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.