1

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?

10
  • In the Python-3.4.5\Lib directory, is there a file named libpython34.a or python34.lib or similar? Commented Jun 5, 2017 at 11:46
  • Also, don't use the -shared flag when linking, that's for creating shared libraries. Commented Jun 5, 2017 at 11:47
  • @Someprogrammerdude No. But there is libpython34.py Commented Jun 5, 2017 at 11:47
  • @Someprogrammerdude That did not change anything Commented Jun 5, 2017 at 11:48
  • Are you sure that it's libpython34.py, with the .py ending? That's not a linker library, it seems that your installation is incomplete. Commented Jun 5, 2017 at 11:49

1 Answer 1

2

I am using VSCode in windows with these arguments to GCC calls while building the c source.

gcc<space>-g<space>-o<space>"project_folder"<space>-IC:/Python37/include/<space>-LC:/Python37/libs/<space>'path/to/your/source.c'<space>-lpython37 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.