dstogov/php-ffi

Is there any way to use c++ api?

morozovsk opened this issue ยท 7 comments

I would like to use php-ffi for opencv but opencv doesn't support C api.
A lot of libraries also support only cpp api.

FFI doesn't provide special support for C++.
I'm not sure, if Python (CFFI) supports C++, but Python has opencv API implemented.
It would be interesting to analize, how they do it, but I'm very limited with time, now.

OP: I'm about to dive into this issue tomorrow for a work project. The answer isn't quite so simple due to C++ Name Mangling, and possible ABI differences.

I have a hunch that if you know the names given to your library's symbols, you could fudge it, but I'd never suggest doing that for anything public or long-lived. See: readelf and objdump

My other idea is to write utility functions either into a surrogate shared library or into the CPP library itself and make them available with extern.

Edit:

Crap. Somebody actually has the account "op" โ˜น๏ธ

cover cpp api to c api,then ffi call c api.

@dstogov:

FFI doesn't provide special support for C++.
I'm not sure, if Python (CFFI) supports C++, but Python has opencv API implemented.
It would be interesting to analize, how they do it, but I'm very limited with time, now.

node-ffi:

node-ffi is a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code.

Requirements: libffi

So node-ffi uses libffi and supports c++ out of the box, yes?

nodejs won again :)

Referring to C as C++ is a common mistake by people not familiar with both languages.

From some of the issue threads in node-ffi I'm not given the impression that all C++ is supported "out of the box", only things like Node's Buffer. The reason it is supported is that Node, itself, is written C++. They don't have a choice but to at least support that type (as that's the best binary representation they have). PHP is in C, so this just never came up yet.

See issue where a user mentions that you have to get the function name from the DLL (you can do this with readelf or objdump as per my comment above).

See their tutorial. They leave objects as void* because "we don't know what the layout" of it is.

C++ is "supported", but only the extern functions. You have to provide a function interface, not a class interface.

Nek- commented

Hello @derrekbertrand does it means that it's impossible to use a library in C++ that only uses classes ? I'm trying to use Box2D that has a (working) PR to make it shared for a POC but I have to say I'm stuck for now.

@Nek- You definitely can if you have a copy of the symbols and know what you're compiling against, etc. But this is an exercise that is particular to each compiler and version of the libraries you're using, so it isn't something often undertaken by FFI projects - it is something for you to figure out.

IMHO, the easiest way is to write function wrappers in C++, declared extern "C", and then use that instead of the class based API.