Generally speaking, PHP speed is faster, but for some more advanced developers, if you want to pursue faster speed, there is no doubt that you can write the C code, and compiled into a dynamic link library (often a. so file), and then PHP by creating a new extension (extension), The. So file is invoked in the extension and the PHP function interface is exposed externally.
In practice, a faster C-function service at the bottom can be used whenever the function interface is invoked.
One, dynamic link library (shared)
The filename suffix for a dynamic-link library is usually ". So". In a Windows system, its filename suffix is ". dll".
If the program is linked to a dynamic connection library, the program needs to be able to find the corresponding dynamic link library file when it runs.
Programs compiled with dynamically linked inventory require the user's machine to also have the corresponding dynamic-link library files installed at run time, which need to be placed in a specific directory to allow the program to load the libraries.
While this does not seem to be easy to use with a static link library program, it reduces the size of the program. For libraries that are used by many programs, the benefits of using dynamic links are even more pronounced.
Production of dynamic Link libraries:
Gcc-shared-fpic-o libmylib.so MYLIB.C ; # compiled into shared library
Option-fpic is required on AMD64 and other platforms are not necessary.
Contains static link libraries to dynamic links Couchen
When compiling a dynamic link library, if you need to link to a static library and include the contents of the link library in the dynamic library that you want to compile, you can use the option-wl,--whole-archive.
For example:
Gcc-shared-o libmylib.so-wl,--whole-archive libmylib.a \
-wl,--no-whole-archive libother.a
The-WL indicated above is passed to the linker (linker).
Second, call dynamic C + + link library
below, the development environment background of this article is CentOS release 6.5. In order to be able to call C library, our PHP 5.6.9,apache 2.4 are downloaded source code and compiled, not directly through the Yum installation! Please note that. As for PHP and Apache source code compilation This article does not mention, as long as the attention in the Configure open the appropriate switch.
The specific steps are as follows:
Add shared libraries. So to the system configuration (assuming the shared library name is ' libhello.so ')
CP libhello.so/usr/local/lib
echo/usr/local/lib >/etc/ld.so.conf.d/local.conf
/sbin/ldconfig
Create an extension header file under the Php/ext directory named Myfunctions.def
Fill in the C function declaration in the file. One row for each function.
string Hello (int a)
int hello_add (int a, int b)
Using Ext_skel to build an expanded skeleton
./ext_skel--extname=myfunctions--proto=myfunctions.def
Turn on the Enable switch in CONFIG.M4
Php_arg_enable (myfunctions, whether to ENABLE myfunctions support,
[--enable-myfunctions Include myfunctions Support])
The extension skeleton has been built up, the following reconfiguration of PHP (below is my personal profile, the reader needs to be modified with their own situation)
/buildconf--force//Generate new Configuration script './configure '--prefix=/usr/local/php ' =lib64 '--enable-fpm '--with-fpm-user=php-fpm '--with-fpm-group=www--enable-mysqlnd '--with-mysql=mysqlnd ' With-mysqli=mysqlnd '--with-pdo-mysql=mysqlnd '--enable-opcache '--enable-pcntl '--enable-mbstring ' Enable-soap '--enable-zip ', '--enable-calendar ', '--enable-bcmath '--enable-exif '--enable-ftp ', '--enable-intl ' With-openssl '--with-zlib '--with-curl '--with-gd '--with-zlib-dir=/usr/lib '--with-png-dir=/usr/lib ' With-jpeg-dir=/usr/lib '--with-gettext '--with-mhash '--with-ldap '--disable-fileinfo =/usr/local/php/etc '--with-apxs2=/usr/local/httpd/bin/apxs '--enable-myfunctions '//configure
Remember! Must add-enable-myfunctions at the end. This way will be compiled into PHP.
When the extension is compiled, you can begin to modify the MYFUNCTIONS.C file in the extension, in which you can add a php->c transfer function that can be invoked in the transfer function.
For example, to add a hello_add php function, which can call the C function add (int a, int b)
A. Add a function declaration
b. Adding PHP functions
Php_function (Hello_add) {...}
Notice that in this function, if you call the interface function in the. So file, you will specify the. So shared library that you are using when make, and the shared library must complete the action added to the system configuration in step 1th.
If you call the. so file, add the Php/makefile to the
Extra_ldflag =-lhello//corresponding to front libhello.so
extra_libs =-lhello
(make Clean)
Every time you finish modifying the C file, make it again.
Restart the Apache server
New extensions can be seen in phpinfo, and functions in the new extension can be invoked directly in PHP.