f2py给出了“'foo'的重新定义...先前的定义在这里". [英] f2py giving "redefinition of 'foo'... previous definition was here"

查看:74
本文介绍了f2py给出了“'foo'的重新定义...先前的定义在这里".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,对我很好.

this is my first question so be nice to me.

我正在使用numpy 1.6.1中的f2py.我有一个fortran模块,其中包含几个可以很好地编译(和工作)的子例程.但是,其中之一使用了erf(x)函数,它是GNU扩展.就我的目的而言,它不够准确,因此我尝试使用外部erf实现.

I'm using f2py from numpy 1.6.1. I have a fortran module containing several subroutines which compile (and work) just fine. However, one of them uses the erf(x) function which is a GNU extension. It is not accurate enough for my purposes, so I am trying to use an external erf implementation.

我正在尝试使用fortran 77中数字食谱"中的一个-在这里我已将所有相关功能复制到与模块相同的文件夹中的一个名为"erf.f"的文件中.我在模块文件顶部使用include 'erf.f'.我将erf函数的实际名称更改为'derf',因此它与gfortran erf扩展名没有冲突.

I am trying to use one from Numerical Recipes in fortran 77 - where I have copied all the relevant functions into one file called 'erf.f' in the same folder as my module. I use include 'erf.f' at the top of my module file. The actual name of the erf function I changed to 'derf', so it doesn't clash with the gfortran erf extension.

但是,当我尝试通过f2py进行编译时,'erf.f'中的每个函数foo都给出了错误

However, when I try to compile via f2py, every function foo in the 'erf.f' is given the error

错误:"foo"的重新定义 错误:"foo"的先前定义在这里

error: redefinition of ‘foo’ error: previous definition of ‘foo’ was here

当我查看提到的包含重新定义和先前定义的c文件时,似乎函数存在两次.我就是不知道为什么?

When I look at the c file which is mentioned to contain the redefinition and the previous definition, it does seem that the function is there twice. I just don't know why?

有人知道如何解决此问题吗?干杯.

Does anybody know how to fix this? Cheers.

我没有提到(因为我认为这是不必要的额外信息),numpy distutils使用f2py来创建扩展.我现在提到它的原因是,我发现可以使用f2py -c my_module.f90 -m mod创建扩展名,但是在运行python setup.py install时,出现了上面详述的错误.那么,通过distutils运行时,f2py有什么不同之处?

I failed to mention (as I thought it unnecessary extra info) that f2py is being used by the numpy distutils to create the extension. The reason I mention it now is that I find that I can create the extension fine with f2py -c my_module.f90 -m mod, but when running python setup.py install I get the error I detailed above. So what does f2py do differently when run through distutils?

编辑#2:如果将外部erf函数文件的所有内容放入与模块相同的文件中,则一切正常.我真的不想要这样做,因为展望未来,我将拥有一个文件的怪异之处,但是直到我对这个问题的答案得到解决之前,它都将一直有效.

EDIT #2: If I put all the contents of the external erf function file into the same file as my module, then everything works. I don't really want to do this because looking into the future I'm going to have a monstrosity of a file, but it'll work for now until I get an answer to this question.

推荐答案

一种解决方案是创建一个模块erf.f90,并使用use erf(或其名称)将其导入您的主代码中.

One solution would be to create a module erf.f90 and import it in your main code using use erf (or whatever it's name is).

我在导入以.f扩展名命名的模块时遇到了一些奇怪的问题,您可以通过将erf.f重命名为erf.f90并在编译时指定-ffixed-form来使它正常工作与gfortran.

I have had some strange problems with f2py importing modules named with a .f extension, you might be able to get it to work simply by renaming erf.f to erf.f90 and specifying -ffixed-form when you compile with gfortran.

如果使用use导入模块,则无需也使用include. include基本上在主代码的源代码中包含了erf.f的实际代码(尽管您注意到,它的行为与直接在主文件中键入erf.f完全不同),而use告诉编译器寻找预编译的模块.

If you import the module using use, you do not need to also use include. include is basically including the actual code of erf.f in the source of your main code (although as you noticed, it doesn't behave exactly the same as typing erf.f directly into the main file), while use tells the compiler to look for a pre-compiled module.

我发现use在使用模块和f2py时对我来说效果很好. (我的代码基本上是Fortran 90的固定形式).假设一个主文件main.f和一个模块subs.f90(确保subs.f90中有module...end module,我将使用以下顺序进行编译:

I have found that use works well for me when working with modules and f2py. (my code is basically fixed form Fortran 90). Assuming a main file main.f, and a module subs.f90 (make sure there is module...end module in subs.f90, I would use the following sequence for compilation:

gfortran -ffixed-form -c subs.f90
f2py.py -c -m main -I/path/to/subs /path/to/subs/subs.f90 main.f

请注意,根据您的系统,可能需要为f2py指定其他选项.对我而言,在Windows上使用MinGW,我需要--compiler=mingw32,因为f2py似乎无法找到C编译器.

Note that you might need to specify other options to f2py depending on your system. For me, with MinGW on Windows, I need --compiler=mingw32, since f2py doesn't seem to be able to locate the C compiler otherwise.

这篇关于f2py给出了“'foo'的重新定义...先前的定义在这里".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆