我正在尝试用python实现文件行计数程序,用c语言编写所需的c代码,并在Linux下编译生成一个.so文件,该程序运行正常。你知道吗
但是当我在windows中编译它时,生成的.dll文件并没有按预期工作(我从Cygwin下载了我的C编译器)。你知道吗
这是我的C代码:
#include <stdio.h>
#include <stdlib.h>
int counter(char file_name[]);
int counter(char file_name[]){
char ch;
FILE *fp;
int l=0;
fp = fopen(file_name,"r");
while( ( ch = fgetc(fp) ) != EOF )
{
if ((ch) == '\n')
{
l=l+1;
}
}
fclose(fp);
return l;
}
我编译了上面的代码以生成共享文件(.so/.dll)
下面是我的python代码:
from ctypes import *
#load the shared object file
myp = CDLL('./myp.so')
res_counter=myp.counter
i=raw_input("Enter File Path: ")
res_counter.restype=c_int
out=res_counter(i)
print out
在ubuntu中编译PFB-th
subbi@subbi-VirtualBox:~/Desktop/wctester/py2c$ ls
myp.c myp.so test2.py
subbi@subbi-VirtualBox:~/Desktop/wctester/py2c$ gcc -shared -o myp.so myp.c
/usr/bin/ld: /tmp/ccVAX8jf.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/ccVAX8jf.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
subbi@subbi-VirtualBox:~/Desktop/wctester/py2c$ gcc -shared -o myp.so -fPIC myp.c
subbi@subbi-VirtualBox:~/Desktop/wctester/py2c$ python test2.py
Enter File Path: ../bar
3
这是我的windows编译(我改了myp.so公司至myp.dll在.py中(对于windows)
[python2] C:\Users\Subbi Reddy\Desktop\py2c\py2c>ls
myp.c test2.py text.txt
[python2] C:\Users\Subbi Reddy\Desktop\py2c\py2c>gcc -shared -o myp.dll -fPIC my
p.c
myp.c:1:0: warning: -fPIC ignored for target (all code is position independent)
#include <stdio.h>
^
[python2] C:\Users\Subbi Reddy\Desktop\py2c\py2c>python test2.py
Enter File Path: text.txt
[python2] C:\Users\Subbi Reddy\Desktop\py2c\py2c>
窗口不显示结果(行数)
我使用mingw32 gcc(4.7.1)编译了您的代码。很好用。你知道吗
编辑: 我也试过和cygwin在一起。在那里也很管用。你知道吗
相关问题 更多 >
编程相关推荐