简体   繁体   English

php-cpp适用于C库吗?

[英]Does php-cpp work for C library?

I have come across a C library from which I want to use functions inside PHP. 我遇到了一个C库 ,我希望在其中使用PHP中的函数。 This question has put me on track of php-cpp. 这个问题让我跟上了php-cpp。 But it is not very clear to me anywhere if I can use php-cpp for pure C. 但是如果我可以将php-cpp用于纯C,那对我来说并不是很清楚。

Most sources on the internet say it's trivial to mix C with C++, so I want to know if it's worth investing my time learning what I need to learn to achieve the goal. 互联网上的大多数消息人士都认为将C与C ++混合起来是微不足道的,所以我想知道是否值得花时间学习我需要学习的东西来实现目标。

But it is not very clear to me anywhere if I can use php-cpp for pure C. 但是如果我可以将php-cpp用于纯C,那对我来说并不是很清楚。

If you try to include any C++ code that actually has C++ features that C does not support in a C program. 如果您尝试包含任何实际具有C语言功能但C语言中不支持的C ++代码。 It simply won't compile. 它根本不会编译。

Most sources on the internet say it's trivial to mix C with C++ 互联网上的大多数消息人士都认为将C与C ++混合起来是微不足道的

That is only the case when you including C code to a C++ program. 只有当您将C代码包含在C ++程序中时才会出现这种情况。 Even then, there are a few exceptions. 即便如此,也有一些例外。

If you are doing this because you do not know C++, you can write a C++ program as if it were C (with a few exceptions, like void pointers) if you are careful enough. 如果你这样做是因为你不了解C ++,你可以编写一个C ++程序,好像它是C(有一些例外,如void指针),如果你足够小心的话。

PHP-CPP is a C++ library that can be used to develop PHP extensions. PHP-CPP是一个C ++库,可用于开发PHP扩展。 It is not required to develop PHP extensions, although it may make your life a little easier. 要求开发PHP扩展,虽然它可能让你的生活变得更轻松。 If you don't know C++, or if you don't want to use it, you can safely ignore PHP-CPP. 如果您不了解C ++,或者您不想使用它,则可以放心地忽略PHP-CPP。 Refer to " Getting Started with PHP Extension Development " for some resources on developing PHP extensions (in C). 有关开发PHP扩展的一些资源(在C中),请参阅“ PHP扩展开发入门 ”。

It is perfectly possible to call C libraries from C++. 完全可以从C ++调用C库。 In most cases, the exact same syntax can be used; 在大多数情况下,可以使用完全相同的语法; at most, you may need to wrap C header files with extern "C" { ... } . 最多,您可能需要使用extern "C" { ... }包装C头文件。

Yes, you can mix C into C++ within PHP-CPP, however it is not a good approach for beginners because you have to understand the related API's for both. 是的,您可以在PHP-CPP中将C混合到C ++中,但对于初学者来说这不是一个好方法,因为您必须了解两者的相关API。

For those up to the task, you will need to include the appropriate header files from PHP as if you were writing a standard C extension, such as 对于那些完成任务的人,您需要包含来自PHP的相应头文件,就像您编写标准C扩展名一样,例如

#include <php.h>
#include <php_globals.h>
#include <php_main.h>
#include <php_network.h>
// etc

You will also need to modify the Makefile for your PHP-CPP project and add the accurate include dirs for your PHP source location, for me its 您还需要修改PHP-CPP项目的Makefile,并为您的PHP源位置添加准确的包括dirs,对我而言

COMPILER_FLAGS = -I/usr/local/include/php -I/usr/local/include/php/main ... etc ... -Wall -c -O2 -std=c++11 -fpic -o

Add standard PHP/ZEND API C code into your PHP-CPP functions/classes 将标准PHP / ZEND API C代码添加到PHP-CPP函数/类中

Php::Value example(Php::Parameters &params)
{
    zval myval;
    zend_string *hello, *world;

    hello = zend_string_init("hello", strlen("hello"), 0);

    /* Stores the string into the zval */
    ZVAL_STR(&myval, hello);

    /* Reads the C string, from the zend_string from the zval */
    php_printf("The string is %s", Z_STRVAL(myval));

    world = zend_string_init("world", strlen("world"), 0);

    /* Changes the zend_string into myval : replaces it by another one */
    Z_STR(myval) = world;
    zend_string_release(hello);
    zend_string_release(world);

    return Php::Value(Z_STRVAL(myval));
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM