PHP Dynamic link library _php tutorial, PHP DLL.

Source: Internet
Author: User

PHP dynamic link library in a call to C + + production of the tutorial, PHP DLL


Generally speaking, PHP speed is faster, but for some of the more advanced developers, if you want to pursue faster speed, there is no doubt that you can write the C code by themselves, and compiled into a dynamic link library (often the. so file), and then PHP by creating a new extension (extension), The. So file is called in the extension, and the PHP function interface is exposed externally.
In practice, the underlying faster C function service can be used whenever the function interface is called.

I. Dynamic link library (shared)

The filename suffix of 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 locate the corresponding dynamic link library file when it runs.

Programs compiled with dynamic link inventory require that the corresponding dynamic link library files are also installed on the user's machine at run time, and that the library files need to be placed in a specific directory so that the program can load the libraries.

Although this does not seem to be easy to use with a static link library, 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.

The creation of dynamic link library:

Gcc-shared-fpic-o libmylib.so MYLIB.C  ; # compiled into a shared library

Option-fpic is required on AMD64, other platforms are not necessary.

Includes static link library to dynamic link library

When compiling a dynamic-link library, if you need to link a static library and include the contents of the link library in a dynamic library to be compiled, 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 above-WL means passed to Linker (linker).

Second, call the 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 is downloaded source code and compiled, not directly through Yum installation! Please note that. As for the PHP and Apache source code compiled this article does not mention, as long as the attention in configure open the appropriate switch.

The steps are as follows:
Add the shared library. So into the system configuration (assuming that the shared library is named ' 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
Complete the C function declaration in the file. One row per function.

string Hello (int a) int hello_add (int a, int b)

Using Ext_skel to build an extended skeleton

./ext_skel--extname=myfunctions--proto=myfunctions.def

Open the Enable switch in the CONFIG.M4

Php_arg_enable (myfunctions, whether to ENABLE myfunctions support,  [--enable-myfunctions        Include myfunctions Support])

Above the extension skeleton is set up, the following reconfiguration of PHP (below is my personal profile, the reader needs to change with their own situation)

./buildconf--force  //Generate new Configuration script './configure '--prefix=/usr/local/php '--with-libdir=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-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 '--with-config-file-path=/usr/local/php/etc '-- With-apxs2=/usr/local/httpd/bin/apxs '--enable-myfunctions '//Configuration

Remember! Be sure to add-enable-myfunctions at the end. This way it will be compiled into PHP.
After the extension has been compiled, you can begin to modify the MYFUNCTIONS.C file in the extension, in which you can add the php->c transfer function, which can be called in the transfer function. So inside the function.
For example, to add a hello_add php function, you can call the C function add (int a, int b)
A. Adding a function declaration

Php_fe (Hello_add, NULL)

b. Adding PHP functions

Php_function (Hello_add) {...}

Note that in this function, if you call the interface function in the. So file, you will need to specify the. So shared library that you are using when you make it, and the shared library must complete the operation that you added to the system configuration in step 1th.
If the. So file is called, then add the Php/makefile in the

Extra_ldflag =-lhello//corresponds to the previous libhello.soextra_libs =-lhello (Make clean)

Every time you finish modifying the C file above, make it again.

Makemake Install

Restart Apache Server

Httpd-k restart

New extensions can be seen in the phpinfo, and functions within the new extension can be called directly in PHP.


Articles you may be interested in:

    • Methods for calling C + + in PHP
    • Expand your PHP to add functionality to your PHP.
    • PHP cannot load dynamic-link libraries such as Php_curl.dll under IIS6/7 under Windows Server

http://www.bkjia.com/PHPjc/1108608.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108608.html techarticle PHP dynamic link library in a call to the tutorial, PHP dll in general, PHP speed is relatively fast, but for some of the more advanced developers, if you want to pursue more ...

  • Related Article

    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.