Failed to connect to fftw3 library

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

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 first tutorial on the fftw3 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 the 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 incorrectly linking to the library, but it is interesting that 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 link correct? Any suggestion would be appreciated.

+6
source share
2 answers

You told the linker where to find the library via -L , but you did not say which library it was referring to. You do the latter by adding -lfftw3 to the end of the line, before -lm .

In addition, the -L flag must be specified after fftwtest.c .

+7
source

You also need to add that you are linking to the fftw library.

Add something like:

 -lfftw 

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

+1
source

Source: https://habr.com/ru/post/974545/


All Articles