0

I am using f2py to wrap some Fortran subroutines. When calling from Python, I always use explicit arrays for the input/output, but have a few working arrays that need to be allocatable. These arrays have no interface with Python. A small test subroutine that replicates the problem is shown below:

subroutine test(n, x, y)

    !f2py intent(in) n, x
    !f2py intent(out) y
    !f2py depend(n) x

    integer, intent(in) :: n, x(n)
    integer, intent(out) :: y

    integer, allocatable :: z(:)

    allocate(z(n))

    z(:) = 10

    y = z(2) + x(1)

end subroutine test

Whenever I use internal allocatable arrays I get the following output when doing "python setup.py develop" with NumPy's distutils package:

test.o : error LNK2019: unresolved external symbol _gfortran_runtime_error referenced in function test_
test.o : error LNK2019: unresolved external symbol _gfortran_runtime_error_at referenced in function test_
test.o : error LNK2019: unresolved external symbol _gfortran_os_error referenced in function test_
ASAP\lib\asap_lib.pyd : fatal error LNK1120: 3 unresolved externals

I am using NumPy's Configuration class from numpy.distutils.misc_utl and gfortran 5.2.0, mingw64, Windows 10.

The program will compile just fine if I use gfortran directly, but when it gets hooked up with f2py (or somewhere in that process) things fail. I can comment out the allocate line and it will compile, but obviously not work at runtime. There are always 3 unresolved externals no matter how many allocatable arrays I have.

While I'm here, I get similar link errors if the Fortran subroutine has a write or print statement in it. I don't need them, would just be useful for debugging if anyone has an idea.

Thanks for the help.

UPDATE 1:

Using the Configuration class from numpy.distutils.misc_util I added an extra link option

config.add_extension('test_lib', sources, extra_link_args=['/FORCE']

after looking at the info here at Microsoft Linker Options. The '/FORCE' is the key option. The print or write command still throw back exit code 255 in Python, but at least the allocatable arrays seem to be working, which is all I cared about.

Seems like a hack, but if anybody knows what is causing the original problem I probably still have a deeper rooted issue that I need to address.

4
  • A wild guess: In your example, does the problem persist if you explicitly deallocate(z) before exiting the subroutine? Commented Feb 21, 2016 at 11:29
  • @ev-br That shouldn't matter. Commented Feb 21, 2016 at 11:38
  • Somehow you are not linking the gfortran runtime library. What happens when you have no allocatable arras, but a print statement or something similar in the code? Try to make a simple dll with simple f2py -c -m module_name. Commented Feb 21, 2016 at 11:40
  • @VladimirF Thanks for the suggestion. A print statement throws the same error, and using f2py -c -m test test.f90 gives the same error also. I should add I have installed the VC++ Compiler for Python 2.7 and using 64-bit Anaconda Python. One thing I had to do was manually point to vcvarsal.bat in the C:\Anaconda\disutils\mscv9compiler.py file (in case that gives you any clues). I've noticed other packages I've installed with f2py that use matmul give similar errors, so I think you've correctly pointed out it's not linking to the runtime lib. Commented Feb 21, 2016 at 15:47

1 Answer 1

1

I had a link problem and specifying compiler=mingw32 in the f2py options fixed all the problems.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.