- MinGW-64
On Windows:
- Download the 64-Bit version of MinGW for Windows from: https://github.com/niXman/mingw-builds-binaries/releases
- Filename:
x86_64-13.2.0-release-win32-seh-msvcrt-rt_v11-rev1.7z
- Extract the 7-ZIP file to:
c:\mingw64
- Add
C:\mingw64\bin
to the system PATH enviornment variable
To specify C linkage, specify extern "C" for your function declarations.
File: example.cpp
extern "C" __declspec( dllexport ) int foo();
int foo() {
return 123;
}
g++ -shared -s -mwindows -Wl,--subsystem,windows -o example.dll example.cpp
The -Wl,--subsystem,windows
isn't really necessary, but it's just conventional that DLLs have the Windows GUI subsystem specified in their PE header. Also note the -s
switch is used to strip symbols from the DLL - you will probably want to do this only for release builds.
Optional: To specify a input directory for header files, just add the -I c:\path\to\include
as parameter:
<?php
use FFI;
$ffi = FFI::cdef(
'int foo();',
'example.dll'
);
var_dump($ffi->foo());
Output:
int(123)