Skip to content

Minimal example describing how to statically link and run C code from PHP

License

Notifications You must be signed in to change notification settings

junlarsen/php-ffi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calling C from PHP

Minimal example showing how to compile and call C from PHP.

In this example, src/lib.c provides a single function named message which just prints some text to the terminal.

Steps

  • Compile C library into shared library
gcc -c src/lib.c -o bin/phplib.o
ar rcs bin/phplib.a bin/phplib.o
gcc -shared bin/phplib.o -o bin/phplib.so
  • Create PHP definitions
<?php

$ffi = FFI::cdef(
    'void message();',
    'bin/phplib.so'
);
  • Call C code from FFI object
$ffi->message();

About

Minimal example describing how to statically link and run C code from PHP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published