PHP擴展調用C++靜態庫


概述

      php通過擴展方式,調用c++源碼,這樣做的理由有很多,當你搜到這篇文章時,相信你已經有自己的考慮了。

寫這篇博客的理由有二:

  1. 整理下php擴展調用c++代碼的過程。網上有很多類似的文章,不過對於php擴展c++的方式,很多文章在關鍵的地方並沒有說明,或者說,大部分都是擴展調用c代碼的。
  2. 已經兩年有余沒寫博客了,在這2017剛開始的時候,開個博客,開始新的旅程,也是不錯的,哈哈。

 

大概目錄

  • c++編譯靜態庫
  • php 擴展編輯
  • 結語

注:下面步驟的操作環境——系統macOS10.12.2,PHP版本 5.6.29.

 c++編譯靜態庫

頭文件:hello.h

#include<string>
std::string hello_joint(std::string a, std::string b);

實現頭文件定義的函數:hello.cpp

 #include "hello.h" 
 std::string hello_joint(std::string a, std::string b)
 {
     std::string str = a+b;
     return str;
 }   

生成hello.o文件

g++ -c hello.cpp

生成靜態庫libhello.a文件

ar -r libhello.a hello.o

寫個簡單的test.cpp測試下:

1 #include<iostream>
2 #include "hello.h"
3 int main()
4 {
5     std::string a = std::string("Hello ");
6     std::string b = std::string("World!");
7     std::cout<<hello_joint(a, b)<<std::endl;
8     return 0;
9 }

編譯

g++ test.cpp libhello.a 

運行

./a.out 

終端輸出

Hello World!

  這樣,你的c++靜態庫制作完成。

 

php 擴展編輯

      如果要編輯php擴展,需要下載php源碼,這里下載的是php-5.6.16。寫文章的時候,才發現php源碼版本和系統的php版本不一致。因為是先下載的php源碼,然后通過brew install php56,不過,影響不大,不用糾結。

運行如下命令,產生擴展文件夾january

./ext_skel --extname=january 

  命名為january,主要是不想跟c++源碼hello產生任何關系,以免后面混淆,當然,也是因為想不出其他比較好的名字。

 

首先編輯config.m4,需要改的地方大概歸結為三處:

  1、找到如下三行,並把前面的注釋dnl去掉。

PHP_ARG_ENABLE(january, whether to enable january support,
Make sure that the comment
is aligned:
[
--enable-january Enable january support])

  2、找到如下的代碼,並在它的下面加上相應代碼,以支持c++調用。

if test "$PHP_JANUARY" != "no"; then
  dnl Write more examples of tests here...

要加上的相應代碼,注意其中的參數名。

if test "$PHP_JANUARY" != "no"; then
  dnl Write more examples of tests here...


  PHP_ADD_INCLUDE(./include)
  PHP_ADD_LIBRARY(stdc++, 1, JANUARY_SHARED_LIBADD)
  PHP_ADD_LIBRARY_WITH_PATH(hello, ./lib, JANUARY_SHARED_LIBADD)
  PHP_REQUIRE_CXX()
  PHP_SUBST(JANUARY_SHARED_LIBADD)

  3、同時,在該函數的最后一行,把january.c改為january.cpp

PHP_NEW_EXTENSION(january, january.cpp, $ext_shared)

 

january.cpp需要做的工作比較多:

  1、重命名:把january.c改為為january.cpp;

  2、在兩處地方加上EXTERN "C"標識,這點不要忘了:

第一處加extern "C"{},並在下面加上需要的c++頭文件:

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"

第二處加BEGIN_EXTERN_C()和END_EXTERN_C():

#ifdef COMPILE_DL_JANUARY
BEGIN_EXTERN_C()       ==>添加的
ZEND_GET_MODULE(january)
END_EXTERN_C()         ==>添加的
#endif

  3、在如下的地方,加入函數名稱,可以理解為php要調用的函數的聲明:

const zend_function_entry january_functions[] = {
    PHP_FE(confirm_january_compiled,    NULL)       /* For testing, remove later. */
    PHP_FE(january_say, NULL)
    PHP_FE_END  /* Must be the last line in january_functions[] */
};

  4、注意到january.cpp中,它編寫了一個函數例子confirm_january_compiled,現在,我們也需要完成january_say的函數實現,它接收來自php的參數,並在函數中調用c++函數。

PHP_FUNCTION(january_say)
{
    char *arg1 = NULL, *arg2 = NULL;
    int arg1_len, arg2_len;
    int argc = ZEND_NUM_ARGS();
    if(zend_parse_parameters(argc TSRMLS_CC, "ss", &arg1, &arg1_len, &arg2, &arg2_len) == FAILURE)
        return;
    std::string a = std::string(arg1);
    std::string b = std::string(arg2);
    std::string res = hello_joint(a, b);
    RETURN_STRING(res.c_str(), res.length());
}

  完成這些步驟之后,剩下的工作就少多了。

 

要接下去做其他步驟的話,建議先做兩個拷貝動作:
  1,新建lib文件夾,將之前制作的libhello.a拷貝到lib里;
  2,新建include文件夾(也可以等到./configure的時候,它會幫你創建這個文件夾),將hello.h頭文件拷貝到這里。

利用php工具,按順序運行如下四個命令:

phpize
./configure
make
make install

  注:如果中間修改了程序代碼什么的,記得要從第一個phpize重新執行來過。

  運行正常的話,january.so會安裝到.../lib/php/extensions/no-debug-non-zts-20131226/目錄下。

 

最后,需要加載january.so,主要是改配置php.ini。如果不知道這個文件在哪的話,你可以在php程序中輸出(echo phpinfo())。

echo phpinfo()的輸出,可以看到指明了php.ini的所在目錄。
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/etc/php/5.6
Loaded Configuration File => /usr/local/etc/php/5.6/php.ini

  首先,找到enable_dl,改為On;

enable_dl = On         --在php.ini 中開啟php動態加載dl

  再找到有很多extension=xxxxxx.so的地方,添加下面句子,加載動態庫,當然也可以在程序中手動加載。

extension=january.so  

  在php程序中測試:

echo january_say("hello ", "world!");

  輸出:

hello world!

 

結語

  快過年了,都不能靜心的好好寫博客了(真的是水平不夠,經驗有限的極好借口\(^o^)/~),如果有錯誤的地方,歡迎指正。

  同時,剛開始做的時候,可能會碰到這樣那樣的問題,希望你最終能順利完成!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM