【发布时间】:2017-06-05 11:43:26
【问题描述】:
我的目录结构如下:
Python-3.4.5
Lib
Include
numpy-master
numpy
scipy-master
scipy
scikit-learn
sklearn
pythoncode.py
pythoncode.c
pythoncode.c 包含:-
#include <Python.h>
void main(int argc, char *argv[])
{
FILE* file;
Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
file = fopen("pythoncode.py","r");
PyRun_SimpleFile(file, "pythoncode.py");
Py_Finalize();
return;
}
pythoncode.py 包含:-
import sklearn, numpy, scipy
我想在 Windows 上使用 Python-3.4.5 运行 pythoncode.py 到 pythoncode.c。我尝试使用这些行:-
gcc -I Python-3.4.5\Include -I Python-3.4.5\PC -c pythoncode.c -w
gcc -shared pythoncode.o -L Python-3.4.5\Lib -w
编译(第一行)有效,但第二行出现以下错误:-
pythoncode.o:pythoncode.c:(.text+0x17): undefined reference to `_imp__Py_SetProgramName'
pythoncode.o:pythoncode.c:(.text+0x1e): undefined reference to `_imp__Py_Initialize'
pythoncode.o:pythoncode.c:(.text+0x32): undefined reference to `_imp__PySys_SetArgv'
pythoncode.o:pythoncode.c:(.text+0x70): undefined reference to `_imp__PyRun_SimpleFileExFlags'
pythoncode.o:pythoncode.c:(.text+0x77): undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
我还尝试了gcc -shared TerrainPredict.o -L Python-3.4.5\Lib -w -lpython34,它给出了另一个错误:-
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lpython34
collect2.exe: error: ld returned 1 exit status
这里有什么问题?正如我所描述的那样运行程序的正确方法是什么。 (目录为直接从网上下载的源码目录)
编辑:-
现在我可以编译和链接pythoncode.c 文件,但是在运行可执行文件时,我得到以下信息:-
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Current thread 0x00007f48fdd6d440 (most recent call first):
Aborted (core dumped)
另外,我的新生成文件是:-
CC=gcc
CCFLAGS=-I Python-3.4.5/Include -I Python-3.4.5 -c -w
LLFLAGS=-o TerrainPredict Python-3.4.5/libpython3.4m.a -lm -lpthread -lutil -ldl
all:
# set PYTHONHOME=Python-3.4.5
$(CC) $(CCFLAGS) TerrainPredict.c
$(CC) TerrainPredict.o $(LLFLAGS)
我在 ubuntu 上构建了 python 并创建了 libpython3.4m.a 文件。
我尝试设置 PYTHONHOME 和 PYTHONPATH 变量,但没有帮助。
我现在做错了什么?
【问题讨论】:
-
在
Python-3.4.5\Lib目录下,是否有一个名为libpython34.a或python34.lib之类的文件? -
另外,链接时不要使用
-shared标志,这是为了创建共享库。 -
@Someprogrammerdude 没有。但是有 libpython34.py
-
@Someprogrammerdude 这并没有改变什么
-
你确定是
libpython34.py,以.py结尾吗?那不是链接库,看来你的安装不完整。
标签: python-3.x gcc makefile runtime-error python-embedding