I have used C code in a Python project like in this tutorial.
I built an extension so that it was possible to call the A function present in the main.c code through Python. However, function A calls other various functions that are present in a file called code.c, and I'm having trouble using those functions.
There are no problems if all the functions are placed in main.c, but I would like to modularize this project for organizational reasons!
The setup.py for building the modules is as follows.
ext = [
Extension(
'main',
sources = ['main.c'] ,
extra_compile_args=['-lpq'] ,
extra_link_args = ['-L/usr/local/pgsql/lib','-lpq'],
language=['c']
)
]
setup(name='project', version='1.0', ext_modules = ext)
How could I modify it so that the code.c functions could be used inside main.c without any problems?
Here is an outline of the situation:
main.c
#include <Python.h>
#include "code.h"
//....
void send(char* name)
{
//DO SOMETHING
function_from_code(name)
}
code.c
.....
#include "code.h"
void function_from_code(char* name)
{
//DO SOMETHING
}
and then the Python code:
import main
...
main.send("My Name")
So in this way, the python code calls the function of module main C (so far so good). At the moment main.c calls a function from code.c, it throws the following error:
ImportError: /usr/local/lib/python2.7/dist-packages/main.so: undefined symbol: function_from_code
Apparently, using #include is not enough.
code.cc-functions that can be called also from Python? In the first case you could just link to "code.c" (either include or like a library) and in the latter case just "import" the "code.c" python module. In case your looking for good answers, could you provide some example of the files and how you want to call them?main.c. And onlymain.ccalls the function incode.c.