Home  >  Article  >  Backend Development  >  Tutorial on calling C/C dynamic link library in PHP, php dynamic link library_PHP tutorial

Tutorial on calling C/C dynamic link library in PHP, php dynamic link library_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:57:31885browse

Tutorial on calling C/C dynamic link library in PHP, php dynamic link library

Generally speaking, php is already faster, but for some more advanced development For readers, if you want to pursue faster speed, there is no doubt that you can write C code yourself and compile it into a dynamic link library (often a .so file), and then PHP creates a new extension (extension). And call the .so file in the extension, and expose the php function interface to the outside world.
In actual use, as long as the function interface is called, the underlying faster C function service can be used.

1. Dynamic link library (shared)

The file name suffix of dynamic link library is usually ".so". In Windows systems, the file name suffix is ​​".dll".

If the program is linked with a dynamic link library, the corresponding dynamic link library file needs to be found when the program is running.

Programs compiled using dynamic link libraries require that corresponding dynamic link library files must be installed on the user's machine when running. These library files need to be placed in specific directories to allow the program to load these libraries.

Although this does not seem to be as convenient as using a program using a static link library, it does reduce the size of the program. For libraries that will be used by many programs, the benefits of using dynamic linking are even more obvious.

Production of dynamic link library:

gcc -shared -fPIC -o libmylib.so mylib.c  ; # 编译成为shared library

The option -fPIC is required on AMD64, but not required on other platforms.

Include static link library into 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 into the 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 -Wl above means passed to the linker.

2. Call the dynamic C/C link library
Below, the development environment background of this article is CentOS release 6.5. In order to be able to call the c library, our php 5.6.9 and apache 2.4 are downloaded and compiled from source code, and cannot be installed directly through yum! Please note. As for the source code compilation of php and apache, this article will not mention it. Just pay attention to turning on the appropriate switches in configure.

The specific steps are as follows:
Add the shared library.so to the system configuration (assuming 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 in the php/ext directory and name it myfunctions.def
Just fill in the c function declaration in this file. One line per function.

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

Use ext_skel to build an extended 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 established above, and php is reconfigured below (the following is my personal configuration file, readers need to modify it based on their own circumstances)

 ./buildconf --force  //生成新配置脚本
 './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-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' '--with-config-file-path=/usr/local/php/etc' '--with-apxs2=/usr/local/httpd/bin/apxs' '--enable-myfunctions' // 配置

Remember! Be sure to add —enable-myfunctions at the end. Only in this way will it be compiled into php.
After the extension is compiled, you can start to modify the myfunctions.c file in the extension. You can add the php->c transfer function in it, and you can call functions in .so in the transfer function.
For example, if you want to add a hello_add php function, you can call the c function add(int a, int b)
a. Add function declaration

PHP_FE(hello_add, NULL)

b. Add php function

PHP_FUNCTION(hello_add){ ... }

Note that in this function, if the interface function in the .so file is called, then when making, you must specify the .so shared library used. The shared library must be added to the .so file in step 1. System configuration operations.
If a .so file is called, add
to php/Makefile

Extra_LDFLAG = -lhello //对应前面的libhello.so
Extra_libs = -lhello
(make clean)

Every time you modify the c file above, you must make again

make
make install

Restart apache server

httpd -k restart

You can see the new extension in phpinfo, and you can directly call the functions in the new extension in php.


Articles you may be interested in:

  • How PHP calls C
  • Use C/C to extend your PHP and add functionality to your PHP
  • PHP under IIS6/7 under Windows server cannot load dynamic link libraries such as php_curl.dll

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1108608.htmlTechArticleTutorial on calling C/C dynamic link library in PHP, php dynamic link library generally speaking, php speed It is already relatively fast, but for some more advanced developers, if they want to pursue more...
Statement:
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