python 调用 C :
还是shell 获得linux 相关比较简单。
目前发现cmd好用。但是不适合很长的shell命令。只能写在脚本哩。。。[最好用的方法应该是???
pythhon 调用C 的时候,向C传递的参数=======发现直接传int不对。。。字符串接收是可以的。
C++ 编译为so 供 python调用,命令:
g++ perm.cpp -fPIC -shared -o perm.so
python
python 代码:
import sys
import os
import glob
import subprocess
import shutil
import traceback
import fcntl
import signal
import ConfigParser
import time
def get_files():
global file
cmd = ["sh", "./test.sh"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
out = process.communicate()
if process.returncode == 0 and out[0].strip() != "NULL":
""" print out
for i in range(len(out)):
print out[i].strip()"""
number_i = out[0].strip()
print number_i
import ctypes
so = ctypes.CDLL("./perm.so")
ret = so.v_perm_v(number_i)
print("the return value is:")
print ret
def main(argv=None):
"""creates a parser for the command line arguments and runs the program for
those arguments"""
get_files()
if __name__ == "__main__":
sys.exit(main())
~
ls /home/vivi | wc -l
ls /home/vivi | wc -l
C代码:
#include
#include
#define elemType int
void arrySwap(elemType arr[], elemType a, elemType b)
{
elemType temp;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
int cnt = 0;
void perm(elemType arr[], int start, int end)
{
if(start == end)
{
cnt++;
printf("%d :/t",cnt);
for(int i = 0; i <= end; i++)
printf("%d ", arr[i]);
printf("\n");
}
else
{
for(int i = start; i <= end; i++)
{
arrySwap(arr, start, i);
perm(arr, start + 1, end);
arrySwap(arr, start, i);
}
}
}
extern "C"
{
int v_perm_v(char* len_c)
{
//printf("len_c = %s\n",len_c);
int len = (int)(len_c[0] - '0');
//printf("len = %d\n", len);
if(len <= 0)
return -1;
int *arr = (int *)malloc(sizeof(int) * len);
if(arr == NULL)
return -1;
for(int i = 0; i < len; i++)
{
arr[i] = i;
//printf("%d ", arr[i]);
}
perm(arr, 0, len - 1);
return 0;
}
}