基于phpx的扩展运行报错undefined symbol

使用phpx写了一个简单的容器,想放到php中运行做点测试。我在这之前已经成功的写了一个dispatch_function注册到了swoole中,而且运行正常。
后面我又写了个C++类,新建了个PHP类,并将C++类的各个方法注册到了PHP类中。编译通过了,但是PHP加载这个扩展却失败了,报错

PHP Warning:  PHP Startup: Unable to load dynamic library '/mypath/mango.so' (tried: /mypath/mango.so (/mypath/mango.so: undefined symbol: _ZN7Context4poolE)...

C++源代码如下

#include <string>
#include <iostream>
#include <unordered_map>

#include "../include/phpx.h"
#include <ext/spl/spl_iterators.h>
#include <server.h>

using namespace std;
using namespace php;

int dispatch_function(swServer *serv, swConnection *conn, swEventData *data); 

class Context {
private:
    static unordered_map<int, char *> pool;
public:
    void static get(Object &_this, Args &args, Variant &retval) {
        int key = args[0].toInt();
        char * value = pool[key];
        if (value == NULL) return;
        retval = value;
    }
    void static set(Object &_this, Args &args, Variant &retval) {
        int key = args[0].toInt();
        char * value = args[1].toCString();
        pool[key] = value;
    }
    void static del(Object &_this, Args &args, Variant &retval) {
        int key = args[0].toInt();
        pool.erase(key);
    }
};
PHPX_EXTENSION()
{
    Extension *ext = new Extension("mango", "0.0.1");

    ext->require("swoole");

    ext->onStart = [ext]
    {
        Class *c = new Class("CppClass");
        c->addMethod("get", Context::get, STATIC);
        c->addMethod("set", Context::set, STATIC);
        c->addMethod("del", Context::del, STATIC);
     
        ext->registerClass(c);

        // 这个函数在PHP中是正常运行的
        swoole_add_function("mango_dispatch_func_1", (void *)dispatch_function);
    };

    return ext;
}
int dispatch_function(swServer *serv, swConnection *conn, swEventData *data)
{
    swConnection *from_sock = swServer_connection_get(serv, conn->from_fd);
    int port = swConnection_get_port(from_sock);
    int worker_id;
    if (port == 80 || port == 8589) {
        // http部分使用无状态轮询worker
        uint32_t key = sw_atomic_fetch_add(&serv->worker_round_id, 1);
        worker_id = key % serv->worker_num;
    } else {
        // 长连接部分使用有状态模式
        worker_id = conn->fd % serv->worker_num;
    }

    return worker_id;
}

MakeFile如下(用的是官方例子中的MakeFile)

PHP_INCLUDE = `php-config --includes`
PHP_LIBS = `php-config --libs`
PHP_LDFLAGS = `php-config --ldflags`
PHP_INCLUDE_DIR = `php-config --include-dir`
PHP_EXTENSION_DIR = `php-config --extension-dir`

mango.so: mango.cpp
    c++ -DHAVE_CONFIG_H -g -o mango.so -O0 -fPIC -shared mango.cpp -std=c++11 ${PHP_INCLUDE} -I${PHP_INCLUDE_DIR} -lphpx\
     -I${PHP_INCLUDE_DIR}/ext/swoole/include -I${PHP_INCLUDE_DIR}/ext/swoole
install: mango.so
    cp mango.so ${PHP_EXTENSION_DIR}/
clean:
    rm *.so

由于本人很多年没摸C++了,基本都还给老师了,不知道哪里出了差错,求大佬帮忙看下是哪里的问题。

蓝山帝景
浏览 697回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP