Search code examples
c++zend-frameworkphp-extension

Undefined reference when build a php extension


I'm trying to make a custom session handler for PHP. (I didn't find a clear documentation about that. If you have a good links, I will be happy to read it ;) )

So when I try to use a zend function like zend_string_init Example

PS_READ_FUNC(mongo)
{
    MongoClient *pMongoClient = static_cast<MongoClient*>(PS_GET_MOD_DATA());
    if(pMongoClient == nullptr)
        return FAILURE;

    std::string strSessionId = (char *)key;
    std::string strSessionData = pMongoClient->read(strSessionId);

    *val = zend_string_init(strSessionData.c_str(), strSessionData.size(), true); //generate a unreference error on __zend_malloc and _emalloc
    return SUCCESS;

}

To build my linker dependency list I use theses line in my makefile

PHP_LINKER_DEPENDENCIES =   $(shell php-config --libs)
> php-config --libs
-lcrypt -lresolv -lcrypt -lz -lpcre -lrt -lm -ldl -lnsl -lxml2 -lssl -lcrypto -lcrypt -lcrypt

The linking command with the error

g++ -L/usr/lib/php/20151012 -L/usr/lib/x86_64-linux-gnu  -o session_storage.so main.o  -lcrypt -lresolv -lcrypt -lz -lpcre -lrt -lm -ldl -lnsl -lxml2 -lssl -lcrypto -lcrypt -lcrypt -L/usr/local/lib -lsasl2 -lssl -lcrypto -lrt -lmongoc-1.0 -lbson-1.0 -lphpcpp

session_storage.o: dans la fonction « zend_string_alloc »:
/usr/include/php/20151012/Zend/zend_string.h:121: référence indéfinie vers « __zend_malloc »
/usr/include/php/20151012/Zend/zend_string.h:121: référence indéfinie vers « _emalloc »

So I don't find the library I need to link with.

Edit about duplicate issue:

I thinks, it's not a duplicate question. The related question is about "what is the error undefined reference ...".

I knows what is this kind of error. I just don't not find the library I need to link with. It's probably a part of zend engine but I'm not sure.


Solution

  • I found the problem. I build my project like an executable but PHP require a library so.... Add -shared to my build option fix the problem. Thanks for your help