2016-09-29 9 views
1

私の目標は、Cグローバル変数を変更することです。Common Lisp&CFFI:グローバル変数を変更する

/* 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 

によって生成された共有ライブラリ内で表示されます私は、次のCヘッダファイルを持っていると仮定すると

私の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) 

私はエラーなしでテストを呼び出すことができますが、私は警告未定義の変数を取得

(setf *global-variable* 42) 

でglobal_variableを変更することはできませんし、次に定義(私は仮定)新しい変数。

共通のlisp(sbcl)でglobal_variableを変更する方法はどうですか?

ありがとうございます!

あなたの構造体の
+0

私は、例えば、C関数を書くことができますvoid set_g(int g){global_variable = g;}、global_variableを設定し、次にset_gのための共通のlispでインタフェースを記述します。しかし、私は別の方法が必要であると仮定します。 – Dimitris

+1

C関数に欠けている 'fclose(fp);'を追加したところ、私にとってはうまくいくようです。 – jkiiski

+0

ありがとうございました! – Dimitris

答えて

0

、このような何かが動作するはずです:

(cffi:with-foreign-object (ptr 'block) 
     ;; setf the slots 
     (setf (cffi:foreign-slot-value ptr 'block 'a) 12) ...etc. 
関連する問題