Separating C++ zend object creation code from module code

php.internals

Andrew Mather

20 years ago
Hi there, I've successfully developed a stripped down C++ php class creator, with private storage, a la Marcus Boerger util.c, and following Jay Smith, George Schlossnagle et al. For neater code, I've stripped out function declarations, function entry tables, class entry etc. into a second .h/.cpp file pair, until all that's left are the static style functions, eg: static void some_object_free_storage(void *object TSRMLS_DC); and static zend_object_value some_object_new_ex(zend_class_entry *class_type, some_object **obj TSRMLS_DC); The nuance of cross-referencing these calls between two .cpp files (listed in config.m4, and so compiled into distinct .o files), eludes me. It's a c/cpp grammar issue, I guess, but one I've never had to deal with till now. Originally I had static foo1() calling static foo2() both in file1.cpp. Now I have static foo1() in file1.cpp calling (static?) foo2() in file2.cpp The correct syntax eludes me I'm afraid, and various static/non-static/extern combinations result either in 'used but not defined' errors, or compile fine but symbol not found, or sigsegv faults, presumably as the 'promised' function is unavailable. (latter output below) Perhaps someone would be kind enough to clarify the syntax appropriate for this call between modules/.o files. Cheers, Andrew.

Sara Golemon

20 years ago
> The nuance of cross-referencing these calls between two .cpp files (listed > in > config.m4, and so compiled into distinct .o files), eludes me. > > It's a c/cpp grammar issue, I guess, but one I've never had to deal with > till > now. >
Quick Lesson: static: Symbols (functions, variables, etc...) declared static are not exported. This means that no other object (source file) has any way of knowing where these symbols are located and thus cannot call them. When a symbol is known not to be used outside of a given source file, it's given the static modifier to avoid polluting the process' symbol space with uneccesary references. It also avoids namespace collisions for more commonly named methods. extern: Typically used in header files with function prototype declarations. It allows one object file to be aware of the prototype and calling semantics for a function in another object file. For example: header.h extern foo(void); extern bar(void); file1.c #include "header.h" void foo(void) { /* do whatever */ } static void local_func(void) { bar(); } int main(int argc, char *argv[]) { local_func(); } file2.c #include "header.h" void bar(void) { foo(); } Same idea for .cpp of course... There are some other things to keep in mind when interfacing between C and C++ (calling semantics) or when developing DLLs for windows (linkage export semantics), but you're most likely not going to have to worry about these as the PHP extension magic pretty much covers this for you. -Sara