C开发者如何使用Swoole

swoole使用cmake来做编译配置,示例程序在examples/server.c中。 您可以在此基础上进行代码开发。 如果需要修改编译细节的选项,请直接修改CMakeLists.txt

生成config.h

swoole依赖phpizeconfigure检测系统环境,生成config.h

cd swoole-src/
phpize
./configure

执行成功后swoole-src目录下会有config.h

Build & Install

cmake .
make
make install

安装路径非系统默认的lib目录时,需要配置ld.so.conf将swoole动态连接库所在的目录添加到link路径中。

#需要root权限
echo "/opt/swoole/lib" >> /etc/ld.so.conf
#或者
echo "/opt/swoole/lib" > /etc/ld.so.conf.d/swoole.conf
#更新动态连接库信息
ldconfig

Example

示例代码:examples/server.c 在C代码中只需要引入swoole头即可。

#include <swoole/Server.h>
#include <swoole/Client.h>

int main() 
{
    swServer serv;
    swServer_create(&serv);
    serv.onStart = my_onStart;
    ...
    swServer_start(&serv);
}

编译运行

gcc -o server server.c -lswoole
./server