使用f90wrap封装Fortran模块遇undefined symbol错误求助
Looks like you're hitting a common linking issue with f90wrap—let's break this down and fix it quickly.
The error undefined symbol: __test_mod_MOD_p happens because when you built the final shared library with f2py-f90wrap, you didn't include the compiled object file of your original Fortran module. The f90wrap-generated code relies on the symbols from your test_mod module, but without linking the test.o file, the shared library can't find them.
Here's the corrected sequence of commands you should run:
- First compile your original Fortran module into a position-independent object file:
gfortran -fPIC -c test.f90
- Generate the wrapper code with f90wrap as you did before:
f90wrap -m test test.f90
- Crucially, include the
test.ofile when building the extension module:
f2py-f90wrap -c -m _test f90wrap_*.f90 test.o
This works because we're now linking the original module's compiled code into the final shared library, so the wrapper can access the p variable from test_mod without missing symbols.
For reference, the test cases in the f90wrap repo likely include the original object files implicitly (or use build systems that handle this automatically), which is why they didn't hit this issue.
内容的提问来源于stack exchange,提问作者abinitio

