Is it possible for Zephir to include an external library?

I have code in C that does some hardware access. This code is ready and well tested. Now I want to implement a web interface for managing this equipment. So I came up with a PHP extension using Zephir.

My question is: β€œIs it possible for Zephir to include an external library accordingly. Link to it?”, And if possible, how can I do this?

+6
source share
1 answer

Yes, it is possible, and there are two approaches for working with C-code.

C code wrapping in CBLOCK

You can embed c-code in tags, for example: %{ // c-code }% .

This function is undocumented, but exists in tests.

https://github.com/phalcon/zephir/blob/master/test/cblock.zep https://github.com/phalcon/zephir/blob/c47ebdb71b18f7d8b182f4da4a9c77f734ee9a71/test/cblock.zep#L16ps . com / phalcon / zephir / blob / c47ebdb71b18f7d8b182f4da4a9c77f734ee9a71 / ext / test / cblock.c

 %{ // include a header #include "headers/functions.h" // c implementation of fibonacci static long fibonacci(long n) { if (n < 2) return n; else return fibonacci(n - 2) + fibonacci(n - 1); } }% 

It looks a little ugly, but it works,) A little more elegant, but also more work, customizable optimizers:

By writing your own optimizer

The optimizer acts as an interceptor for function calls. The optimizer replaces a function call in the PHP user space with direct C-calls, which are faster and have lower performance overheads.

You can write an optimizer with clean interfaces, letting Zephir know the type of parameter passed forward to the C function and the return data type.

Manually: https://docs.zephir-lang.com/en/latest/optimizers.html

Example (calling the fibonacci c-func function): https://github.com/phalcon/zephir/pull/21#issuecomment-26178522

+7
source

Source: https://habr.com/ru/post/975158/


All Articles