使用C++进行PHP扩展

Nyc*_*cto 5 php c++ php-extension

我最近开始考虑编写PHP扩展,并阅读了本文,其中介绍了使用C++构建扩展的起点.当我开始定制时,我遇到了一些试图将一些功能拆分成单独文件的故障.一切都编译和链接没有问题,但当我尝试实际使用扩展时发生错误.确切的信息是:

$ php -dextension=test.so -r "var_dump( new Test );"
php: symbol lookup error: /etc/php/ext/test.so: undefined symbol: _ZN9ContainerI4TestEC1EP17_zend_class_entry
Run Code Online (Sandbox Code Playgroud)

我在两台计算机上试过这个并且都遇到了同样的问题.我知道它找不到Container构造函数的实际实现,但我不知道如何让它在正确的位置查找.

在发布这里之前,我已经尝试尽可能多地减少了绒毛,但是在php界面代码中还有很多瑕疵.代码如下:

config.m4中:

PHP_ARG_ENABLE(test,
    [Whether to enable the "test" extension],
    [  --enable-test      Enable "test" extension support])

if test $PHP_TEST != "no"; then
    PHP_REQUIRE_CXX()
    PHP_SUBST(TEST_SHARED_LIBADD)
    PHP_ADD_LIBRARY(stdc++, 1, TEST_SHARED_LIBADD)
    PHP_NEW_EXTENSION(test, interface.cpp internals.cpp, $ext_shared)
fi
Run Code Online (Sandbox Code Playgroud)

interface.h:

#ifndef INTERFACE_H_
   #define INTERFACE_H_
   #define PHP_TEST_EXTNAME  "test"
   #define PHP_TEST_EXTVER   "0.1"
   #ifdef HAVE_CONFIG_H
      #include "config.h"
   #endif
#endif
Run Code Online (Sandbox Code Playgroud)

interface.cpp:

#include "interface.h"
#include "internals.h"
#include "php.h"

class Test {}; 

extern zend_module_entry test_module_entry;

zend_object_handlers test_object_handlers;

zend_class_entry *test_ce;

void test_free_storage(void *object TSRMLS_DC)
{
    delete (Container<Test> *) object;
}

zend_object_value test_create_handler( zend_class_entry* classInfo TSRMLS_DC )
{
    Container<Test> *obj = new Container<Test>( classInfo );

    zend_object_value retval;
    retval.handle = zend_objects_store_put(
        obj, NULL, test_free_storage, NULL TSRMLS_CC
    );  
    retval.handlers = &test_object_handlers;
    return retval;
}

PHP_METHOD(Test, __construct)
{
    Test* test = new Test;
    Container<Test> *obj = (Container<Test> *) zend_object_store_get_object(getThis() TSRMLS_CC);
    obj->cpp = test;
}

function_entry test_methods[] = { 
    PHP_ME(Test,  __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
    {NULL, NULL, NULL}
};

PHP_MINIT_FUNCTION(test)
{
    zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "Test", test_methods);
    test_ce = zend_register_internal_class(&ce TSRMLS_CC);
    test_ce->create_object = test_create_handler;
    memcpy(
        &test_object_handlers,
        zend_get_std_object_handlers(),
        sizeof(zend_object_handlers)
    );
    test_object_handlers.clone_obj = NULL;
    return SUCCESS;
}

zend_module_entry test_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_TEST_EXTNAME,
    NULL,        /* Functions */
    PHP_MINIT(test),        /* MINIT */
    NULL,        /* MSHUTDOWN */
    NULL,        /* RINIT */
    NULL,        /* RSHUTDOWN */
    NULL,        /* MINFO */
    PHP_TEST_EXTVER,
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_TEST
   extern "C" {
      ZEND_GET_MODULE(test)
   }
#endif
Run Code Online (Sandbox Code Playgroud)

internals.h:

#ifndef INTERNALS_H_
#define INTERNALS_H_
#include "zend.h"

template <class T>
class Container
{
private:
    zend_object zend;
public:
    T* cpp;
    Container ( zend_class_entry* classInfo );
};

#endif /* INTERNALS_H_ */
Run Code Online (Sandbox Code Playgroud)

internals.cpp

#include "internals.h"
#include "zend.h"

template <class T>
Container<T>::Container ( zend_class_entry* classInfo )
   : zend()
{
   zend.ce = classInfo;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下命令构建它:

$ phpize
$ ./configure --enable-test
$ make && make install
$ php -dextension=test.so -r "var_dump( new Test );"
Run Code Online (Sandbox Code Playgroud)

谢谢你尽你所能的帮助

Dav*_*all 3

编译模板类时,必须可从头文件获取实现,因为 C++ 编译器需要模板参数才能编译模板类。如果 C++ 编译器仅编译Internals.cpp,则它将无法创建任何代码,因为类型 T 未知。它只能在interface.cpp的上下文中编译它,但当时编译器无法获得Container的实际实现。

所以问题很简单,Complier 从未被编译过。

您可以简单地将 Compiler 的实现添加到其声明下方,即 insides.h 文件中。