I’m trying to compile C code with python 3.11.5 64 bit with Mingw. I’m using Windows 10. but I’m getting the errors from undefined references. I was able to compile with python3.11 but with 32 bits. But I need it to be 64-bit, because I’ll be calling python functions that use libraries that only 64-bit supports, such as ultralytis.
The code is this:
// main.c
#include <Python.h>
#include <stdlib.h>
#include <stdio.h> // Adding required inclusion for printf
int main() {
// Initialize the Python interpreter
Py_Initialize();
// Import the Python module that contains our functions
PyObject *pModule = PyImport_ImportModule("funcoes_python");
// Checks if the import was successful
if (pModule != NULL) {
// Gets the references to the functions we want to call
PyObject *pFuncSoma = PyObject_GetAttrString(pModule, "soma");
PyObject *pFuncSubtracao = PyObject_GetAttrString(pModule, "subtracao");
// Call the functions and print the results
if (pFuncSoma && PyCallable_Check(pFuncSoma)) {
PyObject *pArgs = PyTuple_Pack(2, PyLong_FromLong(10), PyLong_FromLong(5));
PyObject *pResult = PyObject_CallObject(pFuncSoma, pArgs);
long result = PyLong_AsLong(pResult);
printf("Soma: %ldn", result);
Py_XDECREF(pArgs);
Py_XDECREF(pResult);
}
if (pFuncSubtracao && PyCallable_Check(pFuncSubtracao)) {
PyObject *pArgs = PyTuple_Pack(2, PyLong_FromLong(10), PyLong_FromLong(5));
PyObject *pResult = PyObject_CallObject(pFuncSubtracao, pArgs);
long result = PyLong_AsLong(pResult);
printf("Soma: %ldn", result);
Py_XDECREF(pArgs);
Py_XDECREF(pResult);
}
// Release references
Py_XDECREF(pFuncSoma);
Py_XDECREF(pFuncSubtracao);
} else {
PyErr_Print();
}
// Finaliza o interpretador Python
Py_Finalize();
return 0;
}
The Python functions I’m calling in C are in the code below:
// funcoes_python.c
# funcoes_python.py
def soma(a, b):
return a + b
def subtracao(a, b):
return a - b
But when I compile:
g++ -o main main.c -L C:Usersdossaanaconda3envsautosarlibs -lpython3 -I C:Usersdossaanaconda3envsautosarinclude
I catch these errors:
main.o:main.c:(.text+0x28): undefined reference to `_imp___Py_Dealloc'
main.o:main.c:(.text+0x5c): undefined reference to `_imp__Py_Initialize'
main.o:main.c:(.text+0x6a): undefined reference to `_imp__PyImport_ImportModule'
main.o:main.c:(.text+0x8f): undefined reference to `_imp__PyObject_GetAttrString'
main.o:main.c:(.text+0xa9): undefined reference to `_imp__PyObject_GetAttrString'
main.o:main.c:(.text+0xc3): undefined reference to `_imp__PyCallable_Check'
main.o:main.c:(.text+0xe9): undefined reference to `_imp__PyLong_FromLong'
main.o:main.c:(.text+0xf9): undefined reference to `_imp__PyLong_FromLong'
main.o:main.c:(.text+0x10f): undefined reference to `_imp__PyTuple_Pack'
main.o:main.c:(.text+0x129): undefined reference to `_imp__PyObject_CallObject'
main.o:main.c:(.text+0x13b): undefined reference to `_imp__PyLong_AsLong'
main.o:main.c:(.text+0x181): undefined reference to `_imp__PyCallable_Check'
main.o:main.c:(.text+0x1a7): undefined reference to `_imp__PyLong_FromLong'
main.o:main.c:(.text+0x1b7): undefined reference to `_imp__PyLong_FromLong'
main.o:main.c:(.text+0x1cd): undefined reference to `_imp__PyTuple_Pack'
main.o:main.c:(.text+0x1e7): undefined reference to `_imp__PyObject_CallObject'
main.o:main.c:(.text+0x1f9): undefined reference to `_imp__PyLong_AsLong'
main.o:main.c:(.text+0x24a): undefined reference to `_imp__PyErr_Print'
main.o:main.c:(.text+0x251): undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
Whereas in fact it should have sum and operation outputs. So how should I compile this function call, using python 3.11.5 64 bit?
1