2016-03-26 21 views
1

私はPythonから呼び出し可能になるようにCコードをラップしようとしています。エラーロードモジュール./exp.so未定義シンボル

コード:

#include "stdio.h" 
#include "stdlib.h" 
#include "time.h" 
#include "math.h" 
int givernd(int krp1,int krp2) 
{ 
    int trye; 
    int inp1=krp1; 
    int inp2=krp2; 
    time_t broke; 
    if(inp1 > inp2) 
    { printf("error"); 
     exit(0); 
    } 
    if((inp1)=(inp2-1)) 
    { printf("error"); 
     exit(0); 
    } 
    srand(time(&broke)); 
    trye=rand()%krp2; 
    if(trye<krp1) 
    { 
     return(trye+krp1+1); 
    } 
    if(trye==krp1) 
    { 
     return(trye+1); 
    } 
    return(trye); 
} 

.iファイル:

/* now1.i */ 
%module now1 
%{ 
/* Put header files here or function declarations like below */ 

extern int givernd(int krp1,int krp2); 
%} 

extern int givernd(int krp1,int krp2); 

試験1:

$ swig -python now1.i 
$ gcc -c -fPIC now1_wrap.c -I/usr/include/python2.7 
$ gcc -shared -fpic now1_wrap.o -o now1_wrap.so -lc 
$ python 
Python 2.7.3 (default, Jun 22 2015, 19:43:34) 
[GCC 4.6.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import now1 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
ImportError: dynamic module does not define init function (initnow1) 

試験2:

$ swig -python now1.i 
$ gcc -fpic -I/usr/include/python2.7 -c now1_wrap.c 
$ gcc -shared now1_wrap.o -o now1.so 
$ python 
Python 2.7.3 (default, Jun 22 2015, 19:43:34) 
[GCC 4.6.3] on linux2 
Type "help", "copyright", "credits" or "license" for more  information. 
>>> import now1 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
ImportError: ./now1.so: undefined symbol: givernd 
>>> 

私は間違って何をしていますか?

mainを複製する出発点となるテスト関数を追加する必要があるのだろうと思っていますが、swigマニュアルには必須ではないと書かれています。

答えて

1

giverndの定義を含む最初のファイルは呼び出されていませんが、givernd.cと仮定すると、SWIGが生成したラッパーと同様にコンパイルしてリンクする必要があります。例えば:

$ swig -python now1.i 
$ gcc -c -fPIC now1_wrap.c -I/usr/include/python2.7 -o now1_wrap.o 
$ gcc -c -fPIC givernd.c -I/usr/include/python2.7 -o givernd.o 
$ gcc -shared -fpic now1_wrap.o givernd.o -o now1_wrap.so -lc 

(注:GCCへのお電話でも行方不明になった-o)は

+0

thanks.theチュートリアルはかなり貧弱だったと私は愚かに感謝either.again多くのことを "-o" を追加didntの。 – blute

関連する問題