简体   繁体   中英

How to call C++ “.so” from PHP

I have created a sample ".so" file using C++, which takes two inputs as parameter and return an integer as below.

int calc ( int a , int b ) {

return ( a + b );

}

I need to call this "calc" method in PHP and display it. I searched and found "Zend Engine" could help, but I could get any simple samples to try. Is Zend engine, the only way to do this or is there any other ways to to call C++ ".so" from PHP.

Update:

I have used PHP-CPP for this case.

I followed this tutorial , and I created a ".so" and I could call the function in the ".so" from a test php file from command line. But I copied the same testSkeleton.hph" file to "/var/www" folder and ran from browser, in this case it is not invoking the function from the ".so" file.

You would write a PHP Extension, which implements your functions directly or wraps the calls to other C/C++ libraries.

Some pointers on "How to get started with PHP Extensions writing":

--

Note: basic calculation functions are already implemented :)

More advanced math stuff, too: http://www.php.net/manual/en/book.math.php

<?php
function add($a, $b)
{
    return $a + $b;
}
echo add(5, 10);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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