robinchm
robinchm

Reputation: 109

unable to link to fftw3 library

I am compiling a test program to test the fftw3 (ver3.3.4). Since it is not installed with root previlidge the command I used is:

gcc -lm -L/home/my_name/opt/fftw-3.3.4/lib/ -I/home/my_name/opt/fftw-3.3.4/include/ fftwtest.c

where the library is installed in

/home/my_name/opt/fftw-3.3.4/

My code is the 1st tutorial on fftw3's website:

#include <stdio.h>
#include <fftw3.h>
int main(){
    int n = 10;
    fftw_complex *in, *out;
    fftw_plan p;

    in = (fftw_complex*) fftw_malloc(n*sizeof(fftw_complex));
    out = (fftw_complex*) fftw_malloc(n*sizeof(fftw_complex));
    p = fftw_plan_dft_1d(n, in, out, FFTW_FORWARD, FFTW_ESTIMATE);

    fftw_execute(p); /* repeat as needed */

    fftw_destroy_plan(p);

    fftw_free(in); fftw_free(out);

    return 0;
}

when I compiled the program it returns me following errors:

/tmp/ccFsDL1n.o: In function `main':
fftwtest.c:(.text+0x1d): undefined reference to `fftw_malloc'
fftwtest.c:(.text+0x32): undefined reference to `fftw_malloc'
fftwtest.c:(.text+0x56): undefined reference to `fftw_plan_dft_1d'
fftwtest.c:(.text+0x66): undefined reference to `fftw_execute'
fftwtest.c:(.text+0x72): undefined reference to `fftw_destroy_plan'
fftwtest.c:(.text+0x7e): undefined reference to `fftw_free'
fftwtest.c:(.text+0x8a): undefined reference to `fftw_free'
collect2: ld returned 1 exit status

A quick search implies that I am not linking to the library correctly, but interestingly it does not complain about the declaration of fftw_plan and fftw_complex. In fact if I remove all functions starting with "fftw_", keeping only the declaration, it will pass the compilation.

So where did I go wrong? Is the linking correct? Any suggestion would be appreciated.

Upvotes: 8

Views: 19172

Answers (3)

user1123966
user1123966

Reputation: 208

ok - so I'm not certain of this answer yet, but I've discovered something about the modern fftw (ala 2023). The manual indicates they've intentionally left out some interface declarations.

Compiling, I currently get

:~/Code/Code/apps$ gcc trans1.c -lfftw -lm

/usr/bin/ld: /tmp/cc0OdR2j.o: in function `main':

trans1.c: undefined reference to `fftw_plan_dft_r2c_1d'

/usr/bin/ld: undefined reference to `fftw_execute'

collect2: error: ld returned 1 exit status

:~/Code/Code/apps$ Note my code calls fftw_execute(plan);

To quote their manual: fft.org http://fftw.org/fftw3.pdf
Similar to the legacy Fortran interface (see Section 8.3 [FFTW Execution in Fortran], page 88), we currently recommend not using fftw_execute but rather using the more specialized functions like fftw_execute_dft (see Section 4.6 [New-array Execute Func-tions], page 38)

To prevent you from using fftw_execute by mistake, the fftw3.f03 file does not provide an fftw_execute interface declaration. ! So I'm going to try fftw_execute_dft(plan) etc and see what happens.

fftw_execute .

fftw_execute_dft .

fftw_execute_dft_c2r .

fftw_execute_dft_r2c .

fftw_execute_r2r .

fftw_execute_split_dft .

fftw_execute_split_dft_c2r .

Upvotes: 0

downhillFromHere
downhillFromHere

Reputation: 1977

You have told the linker where to find the library through -L, but you haven't told it which library to link to. The latter you do by adding -lfftw3 at the end of the line, before -lm.

Additionally, the -L flag needs to be listed after fftwtest.c.

Upvotes: 13

Prof. Falken
Prof. Falken

Reputation: 24867

You need to also add that you link to the fftw library.

Add something like:

-lfftw

It depends on what the library file is actually called. (Note how you do that for the math library with -lm.)

Upvotes: 1

Related Questions