无法链接到fftw3库

我正在编译测试程序以测试fftw3(ver3.3.4)。 由于它没有安装root previlidge,我使用的命令是:

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

安装库的位置

 /home/my_name/opt/fftw-3.3.4/ 

我的代码是fftw3网站上的第一个教程:

 #include  #include  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; } 

当我编译程序时它会返回以下错误:

 /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 

快速搜索意味着我没有正确链接到库,但有趣的是它并没有抱怨fftw_plan和fftw_complex的声明。 实际上,如果我删除所有以“fftw_”开头的函数,只保留声明,它将通过编译。

那我哪里出错了? 链接是否正确? 任何建议将不胜感激。

您告诉链接器通过-L在哪里找到库,但您还没有告诉它链接到哪个库。 后者通过在-lm之前的行尾添加-lfftw3来实现。

另外, -L标志需要在fftwtest.c之后fftwtest.c

您还需要添加链接到fftw库的链接。

添加如下内容:

 -lfftw 

这取决于实际调用库文件的内容。 (注意如何使用-lm为数学库执行此操作。)