PHP扩展undefined symbol,基于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++了,基本都还给老师了,不知道哪里出了差错,求大佬帮忙看下是哪里的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值