Using C/C++ code in PHP without writing an extension
Asked Answered
P

6

5

I read it's possible to use C/C++ code in PHP by writing an extension. Is there a simpler way, like wrapping magic or things like that (I must say, I heard the word, but don't know much about what a wrapper is).

Purism answered 6/11, 2011 at 23:59 Comment(0)
H
5

Compiling a php extension isn't very hard. There is a little API to learn just like C programs has the main function and how to pass variables to the main function.

Hector answered 7/11, 2011 at 0:4 Comment(2)
I see, so I won't actually be writing the code again or anything like that?Purism
No, it's a bit like programming a kernel driver. You have to follow some rules.Hector
S
5

No there is not.

PHP parses PHP not C or its decendants like C++

If you want to include C code in php like some function written in C then it has to be called in an extension and it has to be compiled.

A wrapper is code around code. Most any language you use like Delphi, Vb etc. have had native code created that then calls an external API function and in the process handles any type conversion required or parameter fix up.

Sherikasherill answered 7/11, 2011 at 0:6 Comment(0)
O
4

Among others of the same kind, tcc can be used as a C interpreter. You can install it and then, from PHP, send a C program to it :)

$output = `echo -e '#include <stdio.h>\nint main(void) { printf("Hello, World!\\n"); return 0;}' | tcc -run -`;
Orv answered 7/11, 2011 at 0:8 Comment(2)
Can you point me to a tutorial you've liked on this topic? I hope it's reliable and not too complex.Purism
This is not really integrating PHP and C. Using this interpreter solution keeps them distinct. Just read C and/or PHP tutorials and/or manuals. I tried a couple C interpreters before deciding tcc is the one I like best.Orv
A
3

It's not possible to write C or C++ inside of PHP Code. The only way you can go is writing an extension for PHP. Alternatively you can take a look at HipHop-PHP which transforms any PHP code into highly optimized C++ code (it's developed by Facebook).

Astounding answered 7/11, 2011 at 0:4 Comment(2)
Maybe he can write an extension that makes it possible? Like inline ASM.Hector
Inline ASM works in C because C is compiled directly into ASM itself, PHP is not. PHP tranforms into some bytecode which is still far away from being assembler..Astounding
D
1

Depending on what you want to do, you can just compile your code into an executable file and then start it in php e.g. via the exec-function.

If that is not enough, I am afraid you'll have to look into creating an extension - but that's not as hard as it sounds if you already know c or c++.

Decigram answered 7/11, 2011 at 0:21 Comment(0)
V
1

For the record:
Yes you can, using the FFI extension:

<?php
// create FFI object, loading libc and exporting function printf()
$ffi = FFI::cdef(
    "int printf(const char *format, ...);", // this is a regular C declaration
    "libc.so.6");
// call C's printf()
$ffi->printf("Hello %s!\n", "world");
?>

It will return

Hello world!
Vesperal answered 23/3, 2023 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.