I have the following directory structure:
Python-3.4.5
Lib
Include
numpy-master
numpy
scipy-master
scipy
scikit-learn
sklearn
pythoncode.py
pythoncode.c
pythoncode.c contains:-
#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 contains:-
import sklearn, numpy, scipy
I want to run pythoncode.py through pythoncode.c using Python-3.4.5 on Windows. I tried using these lines:-
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
Compilation (1st line) works, but 2nd line gives following error:-
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
I also tried gcc -shared TerrainPredict.o -L Python-3.4.5\Lib -w -lpython34 which gives another error:-
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lpython34
collect2.exe: error: ld returned 1 exit status
What is wrong here? What is the correct way to run the program as I described. (The directories are source directories directly downloaded from web)
EDIT:-
Now I am able to compile and link the pythoncode.c file, but on running the executable I get the following:-
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)
Also, my new makefile is:-
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)
I built python on ubuntu and created the libpython3.4m.a file.
I tried setting PYTHONHOME and PYTHONPATH variables, but that did not help.
What I am doing wrong now?
Python-3.4.5\Libdirectory, is there a file namedlibpython34.aorpython34.libor similar?-sharedflag when linking, that's for creating shared libraries.libpython34.py, with the.pyending? That's not a linker library, it seems that your installation is incomplete.