为什么fftw在vscode中总是未定义引用?

问题描述

我正在尝试在vscode中使用fftw。语言是C ++。我正在WSL中这样做。 我只是通过代码运行器来做到这一点...就像

g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "/home/linhuan/CPP/homework/homework_6/"tempCodeRunnerFile

g ++应该如此,但是它总是向我显示

/usr/bin/ld: sun.cpp:(.text+0xfd): undefined reference to `fftw_malloc'
/usr/bin/ld: sun.cpp:(.text+0x12a): undefined reference to `fftw_plan_dft_1d'
/usr/bin/ld: sun.cpp:(.text+0x1c0): undefined reference to `fftw_execute'
/usr/bin/ld: sun.cpp:(.text+0x1cf): undefined reference to `fftw_destroy_plan'
/usr/bin/ld: sun.cpp:(.text+0x1de): undefined reference to `fftw_free'
/usr/bin/ld: sun.cpp:(.text+0x1ed): undefined reference to `fftw_free'
collect2: error: ld returned 1 exit status

我已经完成了http://www.fftw.org/doc/Installation-on-Unix.html#Installation-on-Unix的工作:

./ configure,make,make install

我不知道该如何处理。我是这个新手...

我的代码是:

#include <complex>
#include <fftw3.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(){
    int N,i=0;
    vector<double> input;
    ifstream data("sunspots.txt");
    double d;
    while (data >> d){
        if(i%2==1){
            input.push_back(d);
        }
        i++;
    }
    N=input.size();
    fftw_complex *in,*out;
    fftw_plan p;
    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    p = fftw_plan_dft_1d(N,in,out,FFTW_FORWARD,FFTW_ESTIMATE);
    for(i=0;i<N;i++){
        in[i][0]=input[i];
        in[i][1]=0.0;
    }
    fftw_execute(p); 
    fftw_destroy_plan(p);
    fftw_free(in); fftw_free(out);
    for(i=0;i<N;i++){
        cout<<i<<in[i][0]<<" "<<in[i][1]<<endl;
    }
    return 0;
}

sunspot.txt是这样的:

0 1
1 2
2 3

解决方法

只要照做

g++ sun.cpp -lfftw3 -lm