ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

phpcpp-安装和运行第一个拓展(一)

2021-06-29 18:00:08  阅读:230  来源: 互联网

标签:php phpcpp extension make 拓展 PHP 安装 name


安装phpcpp

我们不赘述为什么试用phpcpp 进行php拓展库的开发,我们直接进入实践

下载并安装PHP-CPP 库
  • 获取PHP-CPP 库代码
$ git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.git
  • 安装
  1. 切换到PHP-CPP 源代码目录,其中应该包含Makefile 文件,它是一个包含编译器设置和指令的文件,在该目录下运行make启动编译器并构建库
$ make
  1. make install 安装到系统
$ sudo make install
第一个拓展
  • 在官网下载一个空的扩展包demo空demo下载,在demo文件中包含一个 Makefile编译器说明文件,一个特定于扩展的 php.ini 文件,当然还有 *.cpp 文件
  1. Makefile 文件基本不需要修改,我们最主要关注 NAME(拓展名称),INI_DIR(ini 目录);
#
#	Name of your extension
#
#	This is the name of your extension. Based on this extension name, the
#	name of the library file (name.so) and the name of the config file (name.ini)
#	are automatically generated
#

NAME				=	tiway


#
#	Php.ini directories
#
#	In the past, PHP used a single php.ini configuration file. Today, most
#	PHP installations use a conf.d directory that holds a set of config files,
#	one for each extension. Use this variable to specify this directory.
#

INI_DIR				=	/etc/php/7.3/cli/conf.d/
  1. 拓展名.ini 初始拓展名,这个文件主要是在Make 编译的时候要用到里面很简单就是拓展的名称
 extension=tiway.so
  1. main.cpp cpp扩展的实现,我们按照官网尝试写一个输出函数
#include <phpcpp.h>
#include <iostream>

void example()
{
    // the C++ equivalent of the echo() function
    Php::out << "example output" << std::endl;

    // generate output without a newline, and ensure that it is flushed
    Php::out << "example output" << std::flush;

    // or call the flush() method
    Php::out << "example output";
    Php::out.flush();

    // just like all PHP functions, you can call the echo() function 
    // from C++ code as well
    Php::echo("Example output\n");

    Php::Value 
    
}  

/**
 *  tell the compiler that the get_module is a pure C function
 */
extern "C" {
    
    /**
     *  Function that is called by PHP right after the PHP process
     *  has started, and that returns an address of an internal PHP
     *  strucure with all the details and features of your extension
     *
     *  @return void*   a pointer to an address that is understood by PHP
     */
    PHPCPP_EXPORT void *get_module() 
    {
        // static(!) Php::Extension object that should stay in memory
        // for the entire duration of the process (that's why it's static)
        static Php::Extension extension("tiway", "1.0");
        
        // @todo    add your own functions, classes, namespaces to the extension
        extension.add<example>("example");
        
        // return the extension
        return extension;
    }
}

  1. 在文件目录下执行 make && make install
$ make && make install

如果已经编译过则

$ make clean && make && make install

在这里插入图片描述

  1. 查看拓展是否安装成功
php -m | grep tiway

在拓展目录新建php文件,test.php

<?php
example();

//example output
//example outputexample output
?>

总结

  1. 写cpp拓展,至少对cpp 有一定的了解,比如了解cpp 包的引入及常用的包如所用的
  2. PHP-CPP 拓展库目前仅支持linux环境

标签:php,phpcpp,extension,make,拓展,PHP,安装,name
来源: https://blog.csdn.net/qq_39941141/article/details/118337650

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有