PHP call the dynamic link library created by C/C ++

Source: Internet
Author: User
This article mainly introduces the tutorial of calling the dynamic link library produced by CC ++ in PHP. It also briefly mentions how to create a dynamic link library under the gcc compiler, for more information, see php, which is faster. However, for more advanced developers, there is no doubt that you can write c code by yourself and compile it into a dynamic link library (usually. so file), and then php creates a new extension and calls the extension in the extension. so file, and exposes the php function interface.
In actual use, you only need to call this function interface to use the lower-layer c function service.

1. dynamic link library (shared)

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

If the program is connected to the dynamic connection library, you must be able to find the corresponding dynamic link library file when running the program.

During running, programs compiled using dynamic link inventory require that corresponding dynamic link library files be installed on your machine. These library files must be placed in a specific directory, to allow the program to load these libraries.

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

Dynamic link library creation:

Gcc-shared-fPIC-o libmylib. so mylib. c; # compile it into shared library

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

Include static link library to dynamic link library

When compiling a dynamic link library, if you need to link the static library and include the content of the link library to the dynamic library to be compiled, you can use the options-Wl, -- whole-archive.

For example:

gcc -shared -o libmylib.so -Wl,--whole-archive libmylib.a \  -Wl,--no-whole-archive libother.a

The preceding-Wl indicates that the link is passed to the linker (linker ).

II. call the dynamic C/C ++ link Library
The development environment background of this article is CentOS release 6.5. To be able to call the c library, all of our php 5.6.9 and apache 2.4 Download and compile the source code, and cannot be directly installed through yum! Note. The source code compilation of php and apache is not mentioned in this article. you only need to enable the proper switch in configure.

The procedure is as follows:
Add shared library. so to system configuration (assuming the shared library name is 'libello. so ')

 cp libhello.so /usr/local/lib echo /usr/local/lib > /etc/ld.so.conf.d/local.conf /sbin/ldconfig

Create the extension header file in the php/ext directory and name it myfunctions. def.
Enter the c function declaration in this file. Each function has a row.

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

Use ext_skel to build an extension skeleton

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

Enable 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 (below is my personal configuration file, which should be modified according to your 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-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 '// Configure

Remember! -Enable-myfunctions must be added at the end. This will be compiled into php.
After the extension is compiled, you can modify the myfunctions in the extension. in the c file, you can add a php-> c transfer function, which can be called in the transfer function. so.
For example, to add a hello_add php function, you can call the c function add (int a, int B)
A. add a function declaration.

PHP_FE(hello_add, NULL)

B. add a php function

PHP_FUNCTION(hello_add){ ... }

Note: If the function is called. so file interface function, so when you make, you must specify the used. so shared library, which must be added to the system configuration in step 1.
If the. so file is called, add

Extra_LDFLAG =-lhello // corresponds to the previous libhello. soExtra_libs =-lhello (make clean)

Make

makemake install

Restart apache server

httpd -k restart

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

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.