Overview
PHP through the extension method, call C + + source code, this is a lot of reasons, when you search this article, I believe you have your own consideration.
There are two reasons to write this blog:
- Organize the process of calling C + + code in PHP extensions. Well, yes, there are a lot of similar articles on the web, but for the way PHP expands C + +, many of the articles are not explained in key places, or, most of the extensions call C code.
- has not written a blog for more than two years, in the beginning of this 2017, open a blog, start a new journey, is also good, haha.
Approximate directory
- C + + compile static library
- PHP Extended Edit
- Conclusion
Note: The operating environment of the following steps-system macos10.12.2,php version 5.6.29.
C + + compile static library
Header file: hello.h
#include <string>std::string hello_joint (std::string A, std::string b);
Implement the function defined by the header file: hello.cpp
" hello.h " std::string hello_joint (std::string A, std::string b) { std::string str = a +b ; return str;}
Generate HELLO.O File
g++-C Hello. CPP
Generate a static library LIBHELLO.A file
ar -R libhello.a hello.o
Write a simple test.cpp test:
1#include <iostream>2#include"hello.h"3 intMain ()4 {5STD::stringA = std::string("Hello");6STD::stringb = std::string("world!");7Std::cout<Std::endl;8 return 0;9}
Compile
g++ test. CPP
Run
Terminal output
Hello world!
This makes your C + + static library production complete.
PHP Extended Edit
If you want to edit PHP extension, you need to download PHP source code, here is the download php-5.6.16. When writing the article, only to find that the PHP source version and the system version of PHP inconsistent. Because it is the first download of the PHP source code, and then through the Brew install PHP56, however, the impact is small, not tangled.
Run the following command to generate the Extended folder January.
Named January, mainly do not want to have any relationship with C + + source Hello, so as not to confuse the later, of course, but also because I can't think of other better names.
First edit the CONFIG.M4, the need to change the place is probably attributed to three places:
1, find the following three lines, and the previous note dnl removed.
Php_arg_enable (January, whether to ENABLE January support,
is aligned:
[ --enable-january Enable January support])
2, find the following code, and add the corresponding code below it to support C + + calls.
if " $PHP _january " " No " ; then dnl Write More examples of tests here ...
To add the corresponding code, note the name of the parameter.
if " $PHP _january " " No " ; then dnl Write More examples of tests here ... Php_add_include (. /include) php_add_library (stdc1, january_shared_libadd) Php_add_library_ With_path (Hello,. /lib, january_shared_libadd) php_require_cxx () php_subst (January_shared_libadd)
3. At the same time, in the last line of the function, change the JANUARY.C to January.cpp
Php_new_extension (January, January.cpp, $ext _shared)
January.cpp need to do a lot more work:
1. Renaming: Change the january.c to January.cpp;
2, in two places with the extern "C" logo, this does not forget:
Add the extern "C" {} in the first place, and add the required C + + header files below:
extern "C"{#ifdef Have_config_h#include"config.h"#endif#include"Php.h"#include"php_ini.h"#include"ext/standard/info.h"#include"Php_january.h"} #include<string>#include"hello.h"
Second place plus Begin_extern_c () and End_extern_c ():
#ifdef Compile_dl_januarybegin_extern_c () ==> added Zend_get_module (January) End_extern_c () ==> added #endif
3, in the following places, add the function name, can be understood as PHP to invoke the declaration of the function:
Const zend_function_entry january_functions[] = { Php_fe (confirm_january_compiled, NULL) / ** /Php_fe (January_say, NULL) php_fe_end /**/};
4, note that january.cpp, it wrote a function example confirm_january_compiled, now, we also need to complete the January_say function implementation, it receives the parameters from PHP, and calls C + + functions in the function.
php_function (january_say) {Char*ARG1 = NULL, *arg2 =NULL; intArg1_len, Arg2_len; intARGC =Zend_num_args (); if(Zend_parse_parameters (argc tsrmls_cc,"SS", &arg1, &arg1_len, &arg2, &arg2_len) = =FAILURE)return; STD::stringA = std::string(ARG1); STD::stringb = std::string(ARG2); STD::stringres =Hello_joint (A, b); Return_string (Res.c_str (), res.length ());}
After completing these steps, the rest of the work is much less.
To proceed with the other steps, it is recommended that you do two copies of the action first:
1, create a new Lib folder, copy the previously produced Libhello.a to Lib;
2, create a new include folder (you can also wait until the./configure, it will help you build this folder), copy the hello.h header file here.
Using PHP tools, run the following four commands sequentially:
phpize. /Configuremake doinstall
Note: If you change the program code in the middle of something, remember to re-execute from the first phpize.
If it works properly, the january.so will be installed in the./lib/php/extensions/no-debug-non-zts-20131226/directory.
Finally, the need to load january.so, mainly to change the configuration php.ini. If you don't know where the file is, you can output it in a PHP program (Echo phpinfo ()).
echo phpinfo () output, you can see the directory where the php.ini is indicated. Virtual Directory support = disabledconfiguration File (php.ini) Path =/usr/local/etc/php/5.6loaded Configuration File =/usr/local/etc/php/5.6/php.ini
First, find the ENABLE_DL and change to on;
ENABLE_DL = on--open PHP dynamic load DL in php.ini
Then find a lot of extension=xxxxxx.so, add the following sentence, load the dynamic library, of course, can also be manually loaded in the program.
Extension=january.so
Test in a PHP program:
Echo january_say ("Hello", "world!");
Output:
Hello world!
Conclusion
Fast New Year, can not meditate on the well-written blog (really is the level is not enough, experience limited excellent excuse \ (^o^)/~), if there is wrong place, welcome correction.
At the same time, at the beginning of the time, you may encounter such a problem, I hope you can finally complete the successful! Of course, if this blog can help you a little bit, it would be better.
PHP extension calls C + + static Library