What can’t F2PY link with my static library?

  Kiến thức lập trình

I have some fortran subroutines that I’m trying to convert to a Python extension module. One of the subroutines in my fortran code makes a call to another fortran subroutine which is compiled in a static library. I do not have the source code for the static library, as I received it from a third party. Something is wrong with my call to f2py, as the static library isn’t being linked properly. This is my first SO post, and i’m a mechanical engineer with little comp sci background, definitely not a fortran person (more Python/C++), so go easy! Thanks in advance, details below.

In addition to the f2py approach, i have also attempted to create a dll and load it into python using Ctypes, but no luck there either.

I have created simplified versions of the code i’m using below to illustrate my situation. I have verified that this simplified code produces the same error as my real use case.

Here’s the fortran subroutine i’m trying to convert to a python extension module. Note the call to PRINTTEST is the subroutine which lives in the static library printtest.lib.

(initialize.f90):

SUBROUTINE INIT(A,N)

INTEGER N
REAL*8 A

COMMON / FIXIN  / NVAR1, NVAR2, ZVAR3

NVAR1 = 1
NVAR2 = 2
ZVAR3 = 0.65


A = N + 2

CALL PRINTTEST !THIS IS DEFINED IN PRINTTEST.f90/PRINTTEST.lib

END SUBROUTINE INIT

Here’s the definition of PRINTEST for this example, but note that this is compiled into PRINTTEST.lib:
(printtest.lib; I DO NOT HAVE THIS SOURCE IN THE REAL CASE – JUST INCLUDED FOR ILLUSTRATION):

SUBROUTINE PRINTTEST

COMMON / FIXIN  / NVAR1, NVAR2, ZVAR3

print *, 'NVAR1 = ', NVAR1
print *, 'NVAR2 = ', NVAR2
print *, 'ZVAR3 = ', ZVAR3

END SUBROUTINE PRINTTEST

Here’s the f2py command i’m using:
(run_f2py.bat):

python -m numpy.f2py -c initialize.f90^
 -L"%IFORT_COMPILER17%compilerlibintel64"^
 -L"C:UsersmePyCharmProjectsFortranInterface"^
 -l"printtest"^
 -m fort_module^
 --f90exec="C:UsersmeIntelSWToolscompilers_and_libraries_2017.6.270windowsbinintel64ifort.exe"^
 --f90flags="/dll printtest.lib"^
 --verbose

This f2py command generates the following link.exe errors:

initialize.o : error LNK2001: unresolved external symbol printtest_
.fort_module.cp37-win_amd64.pyd : fatal error LNK1120: 1 unresolved externals

The command that f2py sends to the linker, which generates the errors is:

C:Program Files (x86)Microsoft Visual Studio2017EnterpriseVCToolsMSVC14.16.27023binHostX86x64link.exe^
 /nologo^
 /INCREMENTAL:NO^
 /LTCG^
 /DLL^
 /MANIFEST:EMBED,ID=2^
 /MANIFESTUAC:NO^
 /LIBPATH:C:Program Files (x86)IntelSWToolscompilers_and_libraries_2017.6.270windowscompilerlibintel64^
 /LIBPATH:C:UsersmePyCharmProjectsFortranInterface^
 /LIBPATH:C:UsersmeAnaconda3libs^
 /LIBPATH:C:UsersmeAnaconda3PCbuildamd64^
 /LIBPATH:C:Program Files (x86)Microsoft Visual Studio2017EnterpriseVCToolsMSVC14.16.27023ATLMFClibx64^
 /LIBPATH:C:Program Files (x86)Microsoft Visual Studio2017EnterpriseVCToolsMSVC14.16.27023libx64^
 /LIBPATH:C:Program Files (x86)Windows KitsNETFXSDK4.6.1libumx64^
 /LIBPATH:C:Program Files (x86)Windows Kits10lib10.0.17763.0ucrtx64^
 /LIBPATH:C:Program Files (x86)Windows Kits10lib10.0.17763.0umx64^
 printtest.lib python37.lib^
 /EXPORT:PyInit_fort_module^
 C:UsersmeAppDataLocalTemptmpoc9b4k88ReleaseUsersmeAppDataLocalTemptmpoc9b4k88src.win-amd64-3.7fort_modulemodule.obj^
 C:UsersmeAppDataLocalTemptmpoc9b4k88ReleaseUsersmeAppDataLocalTemptmpoc9b4k88src.win-amd64-3.7fortranobject.obj^
 C:UsersmeAppDataLocalTemptmpoc9b4k88Releaseinitialize.o^
 C:UsersmeAppDataLocalTemptmpoc9b4k88ReleaseUsersmeAppDataLocalTemptmpoc9b4k88src.win-amd64-3.7fort_module-f2pywrappers.o^
 /OUT:.fort_module.cp37-win_amd64.pyd^
 /IMPLIB:C:UsersmeAppDataLocalTemptmpoc9b4k88ReleaseUsersmeAppDataLocalTemptmpoc9b4k88src.win-amd64-3.7fort_module.cp37-win_amd64.lib

Clearly, the problem is related to initialize.o, which f2py autogenerates, but i’m not sure how to make it “see” the printtest function signature… Is there something wrong with my f2py set up?

Details about my windows/MSVS/IVF/Python environment:

  • Windows 10
  • MS Visual Studio 2017
  • Intel Visual Fortran 2017
  • Anaconda 3 (Python 3.7)

New contributor

LinharesAJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT