in reply to Wrapping a C shared library with Perl and XS
Here is the C code used for the above example, including build steps.
The header file (xswrap.h)
int mult (int x, int y); void speak (const char* str); unsigned char* arr ();
The .c file (xswrap.c)
#include <stdio.h> #include <stdlib.h> int mult (int x, int y){ return x * y; } void speak (const char* str){ printf("%s\n", str); } unsigned char* arr (){ unsigned char* list = malloc(sizeof(unsigned char) * 3); int i; for (i=0; i<3; i++){ list[i] = i; } return list; }
The entry point file (main.c) for testing the lib
#include <stdio.h> #include "xswrap.h" int main (){ int ret = mult(5, 5); printf("%d\n", ret); speak("hello, world!"); unsigned char* list = arr(); int i; for (i=0; i<3; i++){ printf("%d\n", list[i]); } return 0; }
The build/install script (build.sh)
#!/bin/sh gcc -c -fPIC xswrap.c gcc -shared -fPIC -Wl,-soname,libxswrap.so -o libxswrap.so xswrap.o -l +c sudo cp libxswrap.so /usr/lib sudo cp xswrap.h /usr/local/include
Done!
The library and its header file are both now put into directories in your PATH.
To compile the test program:
gcc -o test main.c -lxswrap
...run it:
./test
You're now ready to wrap the library using Perl and XS per the OP.
|
|---|