(1)c语言代码hello.c:
#include<stdio.h>
int test()
{
printf("hello world");
}
编译方法: gcc hello.c -fPIC -shared -o libhello.so
生成libhello.so
(2)python代码hello.py:
import ctypes
lib = ctypes.cdll.LoadLibrary('./libhello.so')
lib.test()
执行python hello.py报如下错误:
OSError: [WinError 193] %1 不是有效的 Win32 应用程序。
(3)原因:python位数与so位数,so是32位而python是64位。
(4)解决方法:用anaconda保证64位和32位共存
通过conda info命令可以看到当前platform:win-64
首先安装32位python:
set CONDA_FORCE_32BIT=1
conda create -n py27 python=27
切换到32位python环境
set CONDA_FORCE_32BIT=1
activate py32
这时候运行conda info可以看到platform:win-32
再次执行 python hello.py就运行正常了。