I have this kind c/cython project :
project/
├── src/
│ └── modules/
│ ├── cython1.pyx
│ ├── cython1.pxd
│ ├── cython2.pyx
│ ├── cython2.pxd
│ ├── includes/
│ │ ├── c1.h
│ │ ├── c1.c
│ │ ├── d2.h
│ │ ├── c2.c
│ └── ...
└── setup.py
with this setup.py:
filenames = ['module1', 'module2']
extensions = [Extension(name=f"modules.{name}",
sources=[f"modules/{name}.pyx"],
include_dirs=["modules/includes", ]) for name in filenames]
setup(
ext_modules=cythonize(extensions),
packages=['project'],
include_package_data=True,
package_data = {
'module': ['*.pxd', '*.so'],
},
]
)
I have several issues.
in pxd files, I use definitions from c files as follows : cdef extern from "includes/c1.c"
and the project compiles and runs without any issue.
When I try to cimport module1 in another context, I have a fatal error: ‘c1.c’ file not found.
When I setup cdef extern from "includes/c1.h"
in the pxd files, the project compiles but its execution gives errors (functions from c files are not in namespace).
I tried to add corresponding c files to the source list for each module (sources=[f"modules/{name}.pyx", "c1.c"]
), then I get a message saying that c functions are declared multiple times. The fact is that I use c functions in the different cython modules. Moreover, the different c/h files interact with each other (for instance there can be an #include “c1.h” in “c2.c”).
In the end, I just can’t manage it, and can’t figure out how to structure the project so that it runs properly and can also be cimported. I understand from the forums that one solution might be to precompile c file as a shared library, but I can’t get this procedure to work directly in setup.py.
What’s the best way to structure the project and to write the setup.py?