Skip to content

Instantly share code, notes, and snippets.

@vydd vydd/call.lisp Secret
Created Sep 9, 2019

Embed
What would you like to do?
CFFI calling char* with a vector
(ql:quickload :cffi)
(cffi:define-foreign-library vowels (t (:default "/path/to/libvowels")))
(cffi:use-foreign-library vowels)
(defvar *vector* #(104 101 108 108 111))
(print (cffi:foreign-funcall "vowels" :string (map 'string #'code-char *vector*) :int))
# prints out 2
gcc vowels.c -shared -o libvowels.so
#include <string.h>
int vowels(char *str) {
int count = 0;
for (int i = 0; i < strlen(str); ++i) {
switch (str[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
default:
break;
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.