1

私の目標は、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 を変更する方法ですか?

前もって感謝します!

4

1 に答える 1

0

あなたの構造体では、次のようなものがうまくいくはずです:

(cffi:with-foreign-object (ptr 'block)
        ;; setf the slots
        (setf (cffi:foreign-slot-value ptr 'block 'a) 12) ...etc.
于 2017-07-09T12:06:23.773 に答える