PHP Extension with C++ -
i started looking @ writing php extensions , read through this article, describes starting point building extension using c++. i've started customize, i've run hitch trying split of functionality separate file. compiles , links without problems, error occurs when try use extension. exact message is:
$ php -dextension=test.so -r "var_dump( new test );" php: symbol lookup error: /etc/php/ext/test.so: undefined symbol: _zn9containeri4testec1ep17_zend_class_entry i tried on 2 computers , both experience same problem. understand can't find actual implementation container constructor, don't know how in right place.
i've tried cut out of fluff can before posting here, there still lot of cruft in php interface code. code follows:
config.m4:
php_arg_enable(test, [whether enable "test" extension], [ --enable-test enable "test" extension support]) if test $php_test != "no"; 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 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 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 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_ */ internals.cpp
#include "internals.h" #include "zend.h" template <class t> container<t>::container ( zend_class_entry* classinfo ) : zend() { zend.ce = classinfo; } i'm building using following commands:
$ phpize $ ./configure --enable-test $ make && make install $ php -dextension=test.so -r "var_dump( new test );" thanks can offer
when compiling template classes, implementation must available header file since c++ complier needs template arguments in order compile template class. if c++ compiler compile internals.cpp, not able create code type t not known. able compile in context of interface.cpp actual implementation of container not available compiler @ time.
so problem complier never compiled.
you add implementation of compiler below declaration, in internals.h file.
Comments
Post a Comment