Calling C/C++ library function from PHP - How to send a -std=c+11 to compiler
I´ve found a solution. Here is the definitive code:
PHP_ARG_ENABLE(vehicles,
[Whether to enable the "vehicles" extension],
[ --enable-vehicles Enable "vehicles" extension support])
if test $PHP_VEHICLES != "no"; then
CXX_FLAGS="-std=c++0x"
PHP_REQUIRE_CXX()
PHP_SUBST(VEHICLES_SHARED_LIBADD)
PHP_ADD_LIBRARY(stdc++, 1, VEHICLES_SHARED_LIBADD)
PHP_NEW_EXTENSION(vehicles, vehicles.cc car.cc, $ext_shared)
fi
Make sure the CXX_FLAGS
goes before PHP_REQUIRE_CXX()
otherwise it won´t work.
There is also a macro called X_CXX_COMPILE_STDCXX_11([noext], [mandatory])
whose code is here that automates that process.
Calling C/C++ library function from PHP - with multiple classes in a shared library
PHP_ADD_LIBRARY_WITH_PATH
did the trick. Use it as many times as you need to (for adding multiple libraries).
The final code:
PHP_ARG_ENABLE(vehicles,
[Whether to enable the "vehicles" extension],
[ --enable-vehicles Enable "vehicles" extension support])
if test $PHP_VEHICLES != "no"; then
PHP_ADD_LIBRARY_WITH_PATH(libraryname1, /etc/whatever_path_to_library, VEHICLES_SHARED_LIBADD)
PHP_ADD_LIBRARY_WITH_PATH(libraryname2, /etc/whatever_path_to_library, VEHICLES_SHARED_LIBADD)
PHP_ADD_LIBRARY_WITH_PATH(libraryname3, /etc/whatever_path_to_library, VEHICLES_SHARED_LIBADD)
PHP_REQUIRE_CXX()
PHP_SUBST(VEHICLES_SHARED_LIBADD)
PHP_ADD_LIBRARY(stdc++, 1, VEHICLES_SHARED_LIBADD)
PHP_NEW_EXTENSION(vehicles, vehicles.cc car.cc, $ext_shared)
fi
Is it possible to call C code from php
There is a way to do it, but it's probably not a very good idea. One could write a small native program around the library (an executable for your system, that is). Then, you could use the php system() function to call that program. Then, read the output you need (if any) off the standard output?
(there may be a better way, I'm not super-familliar with PHP, but at least this should work.)
calling c from php
If your C code is compiled to an executable, that executable can be executed from PHP -- but I'm guessing this is not quite what you meant ?
If you want to excute C code from PHP : that is not possible... unless you compile your C code to a PHP extension.
A PHP extension is like a .dll
, on windows : it's a library, that can export functions that can be called from PHP.
If you want to learn more about writing PHP extensions, those links will probably interest you :
- Extension Writing Part I: Introduction to PHP and Zend
- Extension Writing Part II: Parameters, Arrays, and ZVALs
- Extension Writing Part II: Parameters, Arrays, and ZVALs [continued]
- Extension Writing Part III: Resources
- And, just for reference, as i'ts about C++ and not C : Wrapping C++ Classes in a PHP Extension
And, if you are really interested by the subject, and ready to spend some money on it, you could buy the book Extending and Embedding PHP (some pages are available as preview on Google Books too) ; It's considered as the book to read when interested on this subject (In fact, I've bought it some time ago, and, in my opinion, it is indeed an interesting read)
BTW, the author of that book is also the author of the first four articles I linked to ;-)
PHP: How To Call Standard Library Functions
Can you package your libraries into a DLL? If so, you can call them through PHP's COM api.
PHP COM Docs:
http://us3.php.net/manual/en/book.com.php
Example Code:
<?php
$com = new COM("DynamicWrapper");
$com->Register("KERNEL32", "Beep", "i=ll", "f=s", "r=l");
$com->Beep(800, 10);
Otherwise you can write a extension that contains a custom wrapper function (ie, execute_through_wrapper('yourfunc')). Here is a doc on writing php functions in C.
http://php.net/manual/en/internals2.funcs.php
Edit:
http://abhinavsingh.com/blog/2008/12/php-extensions-how-and-why/
Here is a quick tutorial on writing extensions in C. It shouldn't be too difficult to write a wrapper function. Once you created the extension, it can be loaded dynamically through dl()
(very dangerous, and depreciated).
http://us2.php.net/manual/en/function.dl.php
Those are the only options in your case. There isn't a linux equivalent (.so loader) of the dll loader (its a win32-related api call).
Is it possible to call functions from a C-library in php from a linux server?
With those specs. you basically have two options.
Create a wrapper library which acts as a PHP extension which enables you to call your C-functions directly from PHP, for example
mylib_awesome_func('hello');
Create a command line utility which acts as an interface to your C library and then call this tool with exec() in PHP.
Option one could be considered more "clean" but is definitely harder, whereas option two may be very easy but may in some cases not be possible depending on what kind of data needs to be transmitted/manipulated back and forth to and from the library.
Related Topics
How to Convert Output of Number_Format Back to Numbers in PHP
Utf-8 Encoded HTML Pages Show Questions Marks Instead of Characters
PHP Recursion to Get All Possibilities of Strings
Best Practices to Test Protected Methods with PHPunit
Save Current Page as HTML to Server
How to Get the Previous Url Using PHP
How to Merge Transparent Png with Image Using PHP
Is It Wise to Use PHP for a Daemon
PHP Date() and Strtotime() Return Wrong Months on 31St
How to Call a Model from a View
Leverage Browser Caching for 3Rd Party Js
How to Call Codeigniter Controller Function from View
PHP HTML Domdocument Getelementbyid Problems
How to Efficiently Find the Closest Locations Nearby a Given Location
How to Bind Parameters to a Raw Db Query in Laravel That's Used on a Model