0

I am learning how to call fftw from fortran. My test program is the following:

Program Single_Thread_FFTW

  use, intrinsic :: iso_c_binding
  implicit none
  integer, parameter :: N=16
  type (c_ptr) :: plan_forward
  integer :: k
  complex (kind=8) :: in(N), out(N)

  include 'fftw3.f03'

  in=(/(sin(3.14159d0*k),k=1,N)/)
  print *,in
  plan_forward=fftw_plan_dft_1d(N,in,out,FFTW_FORWARD,FFTW_ESTIMATE)
  call fftw_execute_dft(plan_forward,in,out)
  print *,out
  call fftw_destroy_plan(plan_forward)

End Program Single_Thread_FFTW

The first confusion came about because the fftw website suggests all the fftw call should be prefaced with the letter d, as in 'dfftw_plan_dft_1d'. However none of the functions/subroutines in fftw3.f03 (located in /usr/include) have the d prefix. When I try to compile/link with either

gfortran -I/usr/include  -o example1  example1.f90     or
gfortran -I/usr/include -L/usr/lib/x86_64-linux-gnu -o example1 example1.f90

the linker throws the errors:

example1.f90:(.text+0xf3): undefined reference to `fftw_plan_dft_1d'
example1.f90:(.text+0x114): undefined reference to `fftw_execute_dft'
example1.f90:(.text+0x1cc): undefined reference to `fftw_destroy_plan'

even though the libraries are in /usr/lib/x86_64-linux-gnu. A separate point is that I would like to lear how to declare double precision complex variables without using the (kind=8) modifier. My compiler (gfortran) doesnt know about double complex.

Clinton Winant
  • 704
  • 10
  • 19
  • I think the first confusion is that you're reading the documentation for use with legacy fortran but are using the modern fortran module (possibly). The actual linker error issue is, I think, because whilst you've told it where the libraries live with the `-L` flag, you've not told it *which* libraries to try to link. Try adding `-lfftw3` (or appropriate similar) to your link command. – d_1999 Jun 19 '17 at 09:10
  • @d_1999 I tried both: 'gfortran -I/usr/include -L/usr/lib/x86_64-linux-gnu -lfftw3 -o example1 example1.f90' and with -lfftw3f, but the undefined reference errors are still there BTW thanks so much. The lib directory has both 'libfftw3.a' and 'libfftw3f.a' – Clinton Winant Jun 19 '17 at 09:18
  • 1
    Can you try putting the `-lfftw` at the end of the link line? – d_1999 Jun 19 '17 at 09:27
  • 1
    They *have to* be at the end of the link line. At least for static libraries. – Vladimir F Героям слава Jun 19 '17 at 10:35
  • BTW using 8 in `kind=8` is ugly and not portable. For FFTW the portable way is `kind=c_double` and `kind=c_double_complex`. See also https://stackoverflow.com/documentation/fortran/939/data-types/4390/precision-of-floating-point-numbers#t=201706191036480449062 – Vladimir F Героям слава Jun 19 '17 at 10:36
  • @d_1999 yes that works Thanks again – Clinton Winant Jun 19 '17 at 11:19
  • @Vladimir 'kind=c_double': This is great, do you have reference to a source like the one you sent, but where there are 'kind= ' for integer, character and logical variables? It would be useful to have all that together in one place? – Clinton Winant Jun 19 '17 at 11:24
  • See also https://stackoverflow.com/questions/3170239/fortran-integer4-vs-integer4-vs-integerkind-4 and consult your favourite Fortran textbook or tutorial. – Vladimir F Героям слава Jun 19 '17 at 11:25

0 Answers0