I am trying to call Python code in C with this code:
#include <stdio.h>
#include <Python.h>
int main()
{
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("print('This is Python in C')");
Py_Finalize();
printf("\n");
return 0;
}
and have tried compiling it with this command:
gcc python_test_in_c.c
However it returns an error returns saying:
undefined referance to `__imp __Py_Initialise`
undefined referance to `__imp__PyRun_SimpleSringFlags`
undefined referance to `__imp__Py_Finalise`
collect2.exe: error: ld returned 1 exit status
What is going wrong? How can I fix this?
Any help would be appreciated
P.S I am not sure, but could this be something to do with the fact I copied the Python 'include' file (containing Python.h) in the include file for MinGW located at C:/MinGW
UPDATE: I have now learned this is ok to do but considered bad practice.
1 Answer 1
You are not linking with the python library...
try:
gcc python_test_in_c.c -lpython3.6m
change 3.6 to your version of choice...
3 Comments
Explore related questions
See similar questions with these tags.
PyRun_SimpleString("print('This is Python in C')");. It is embedding via an library API.fopenWhere no one would say the file contents is embedded into the C code. In fact the question is not even related to Python or possibly C, but linking and there is no information given by the user to really solve it (jut rough guesses).