私の目標は、C グローバル変数を変更することです。
次の C ヘッダー ファイルがあるとします。
/* test.h */
int global_variable;
および C ソース ファイル:
/* test.c */
#include "stdio.h"
#include "test.h"
extern int global_variable;
void test(void) {
FILE *fp;
fp = fopen("output.txt", "w");
fprintf(fp, "Global variable: %d\n", global_variable);
}
global_variable は、によって生成された共有ライブラリ内に正しく表示されます
gcc -c -fPIC test.c
gcc -shared -o libtest.so test.o
私のlispインターフェースは次のようになります。
(ql:quickload :cffi)
(cffi:define-foreign-library libtest
(:unix (:default "./libtest"))
(t (:default "./libtest")))
(cffi:use-foreign-library libtest)
(cffi:defcvar ("global_variable" *global-variable*) :int)
(cffi:defcfun "test" :void )
エラーなしで test を呼び出すことはできますが、global_variable を次のように変更することはできません。
(setf *global-variable* 42)
未定義の変数の警告が表示された後、新しい変数を定義します (と思います)。
問題は、common lisp (sbcl) で global_variable を変更する方法ですか?
前もって感謝します!