Hi I am new to embedding source code from python into C. I have a basic program that creates a cython module called "introduction." The "introduction" module holds one function called "introduction."
cdef char* introduction(char* name, char* lastName):
return "Hello, {} {}!".format(name, lastName).encode("utf-8")
We then call it into this program:
/**
File : embed.c
Project : None
Programmer : Braiden Gole
First version : 2023-04-22
Description : This is my first embeded python program.
NOTE: Where Python is located.
NOTE: C:\Users\<my_username>\AppData\Local\Programs\Python\Python311\include
NOTE: "C:\Users\<my_username>\AppData\Local\Programs\Python\Python311\libs\python311.lib"
NOTE: "C:\Users\<my_username>\AppData\Local\Programs\Python\Python311\python311.dll"
COMPILE:
gcc -c embed.c -o embed.o -IC:/Users/<my_username>/AppData/Local/Programs/Python/Python311/include
LINK:
gcc -o embed embed.o -IC:/Users/<my_username>/AppData/Local/Programs/Python/Python311/include -LC:/Users/<my_username>/AppData/Local/Programs/Python/Python311/libs -lpython311
*/
#include <stdio.h>
#include <Python.h>
int main(int argc, char* argv[])
{
// Initialize the python interpreter.
Py_Initialize();
// Import the CPython module.
PyObject* pModule = PyImport_ImportModule("introduction");
if (!pModule)
{
printf("%s", "Failed to imprt python module.");
return 1;
}
PyObject* pFunction = PyObject_GetAttrString(pModule, "introduction");
if (!pFunction || !PyCallable_Check(pFunction))
{
printf("%s", "Failed to get python function.");
return 1;
}
// Create a tuple and set the arguments of the function.
PyObject* pArguments = PyTuple_New(2);
PyTuple_SetItem(pArguments, 0, PyUnicode_FromString("Braiden"));
PyTuple_SetItem(pArguments, 1, PyUnicode_FromString("Gole"));
PyObject* pResult = PyObject_CallObject(pFunction, pArguments);
Py_DECREF(pArguments);
if (!pResult)
{
printf("%s", "Failed to call python function.");
return 1;
}
char* introduction = PyBytes_AsString(pResult);
printf("%s", introduction);
Py_DECREF(pResult);
// Clean up resources.
Py_DECREF(pFunction);
Py_DECREF(pModule);
Py_Finalize();
return 0;
}
Pay attention to the paths show in the code comments as well as the files that those paths end with. Those are the path values I get from my system so I know those directories and files exist.
I can compile the embed.c
but I cant link the object file embed.o
.
Could you please help me with this.
Things to note:
- Python is installed to PATH
- Have installed pythons debugging symbols
- Have installed pythons debug binaries.
- I just upgraded to Python 3.11.3
- I have tried to link against my old Python installation Python 3.10 and still nothing.
Here is what I get when I try to link the file:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x23): undefined reference to `_imp___Py_Dealloc'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x3b): undefined reference to `_imp__Py_Initialize'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x49): undefined reference to `_imp__PyImport_ImportModule'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x88): undefined reference to `_imp__PyObject_GetAttrString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xa1): undefined reference to `_imp__PyCallable_Check'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xd1): undefined reference to `_imp__PyTuple_New'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xe3): undefined reference to `_imp__PyUnicode_FromString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0xfd): undefined reference to `_imp__PyTuple_SetItem'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x10b): undefined reference to `_imp__PyUnicode_FromString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x125): undefined reference to `_imp__PyTuple_SetItem'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x13b): undefined reference to `_imp__PyObject_CallObject'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x17b): undefined reference to `_imp__PyBytes_AsString'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: embed.o:embed.c:(.text+0x1be): undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
Thanks for your time.