Skip to content

Instantly share code, notes, and snippets.

@vydd

vydd/call.lisp Secret

Created September 9, 2019 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vydd/6484a05906ebc10595f6b7e15b41d048 to your computer and use it in GitHub Desktop.
Save vydd/6484a05906ebc10595f6b7e15b41d048 to your computer and use it in GitHub Desktop.
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