Python 3.x 在Windows中用C语言嵌入python
我有以下目录结构:Python 3.x 在Windows中用C语言嵌入python,python-3.x,gcc,makefile,runtime-error,python-embedding,Python 3.x,Gcc,Makefile,Runtime Error,Python Embedding,我有以下目录结构: 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_SetPr
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;
}
import sklearn, numpy, scipy
我想在Windows上使用Python-3.4.5
通过pythoncode.c
运行pythoncode.py
。我试着用这些句子:-
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
编译(第1行)有效,但第2行给出以下错误:-
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-sharedterrainpredict.o-lpython-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
这里怎么了?如我所述,运行程序的正确方法是什么。(目录是直接从web下载的源目录)
编辑:-
现在,我能够编译并链接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)
另外,我的新makefile是:-
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
变量,但没有效果
我现在做错了什么?在构建c源代码时,我在windows中使用带有这些GCC调用参数的VSCode
gcc<space>-g<space>-o<space>"project_folder"<space>-IC:/Python37/include/<space>-LC:/Python37/libs/<space>'path/to/your/source.c'<space>-lpython37
gcc-g-o“项目文件夹”-IC:/Python37/include/-LC:/Python37/libs/'path/to/your/source.c'-lpython37
在Python-3.4.5\Lib
目录中,是否有名为libpython34.a
或python34.Lib
或类似文件?此外,链接时不要使用-shared
标志,这是用来创建共享库的。@Someprogrammerdude没有。但是有libpython34。py@Someprogrammerdude这并没有改变任何事情。您确定它是libpython34.py
,并以.py
结尾吗?这不是链接器库,您的安装似乎不完整。