C++ 无法在vscode中编译FFTW3库,但同一命令在mac终端中有效

C++ 无法在vscode中编译FFTW3库,但同一命令在mac终端中有效,c++,visual-studio-code,C++,Visual Studio Code,我对FFTW库的第一次测试如下所示 #include<iostream> #include<complex> #include<fftw3.h> #include<stdlib.h> using namespace::std; int main(){ fftw_complex* x; complex<double> y; y.real(1.0);y.imag(-1.0); x = reinterpre

我对FFTW库的第一次测试如下所示

#include<iostream>
#include<complex>
#include<fftw3.h>
#include<stdlib.h>

using namespace::std;

int main(){
    fftw_complex* x;
    complex<double> y;
    y.real(1.0);y.imag(-1.0);
    x = reinterpret_cast<fftw_complex*>(&y);
    cout << (*x)[0] << endl << (*x)[1] << endl;

    double y1[2];
    y1[0] = 2.0;y1[1] = -2.0;
    x = &y1;
    cout << (*x)[0] << endl << (*x)[1] << endl;

    fftw_complex x1;
    x1[0] = 3.0;x1[1] = -3.0;
    cout << x1[1] << endl;

    fftw_complex* z;
    z = (fftw_complex*)fftw_malloc(sizeof(fftw_complex));
    (*z)[0] = 1.0; (*z)[1] = -1.0;
    cout << (*z)[1] << endl;
    fftw_free(z);

    fftw_complex* in, *out;
    fftw_plan p;
    int N = 5;
    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_MEASURE);
    (*in)[0]     =  1.0; (*in)[1]     = 0.0;
    (*(in+1))[0] =  2.0; (*(in+1))[1] = 0.0;
    (*(in+2))[0] =  1.0; (*(in+2))[1] = 0.0;
    (*(in+3))[0] = -1.0; (*(in+3))[1] = 0.0;
    (*(in+4))[0] =  1.5; (*(in+4))[1] = 0.0;
    fftw_execute(p);
    for(int i=0;i<N;i++){
        cout << (*(out+i))[0] << "  " << (*(out+i))[1] << endl;
    }
    p = fftw_plan_dft_1d(N, out, in, FFTW_BACKWARD, FFTW_MEASURE);
    fftw_execute(p);
    for(int i=0;i<N;i++){
        cout << (*(in+i))[0]/N << "  " << (*(in+i))[1]/N << endl;
    }
    fftw_destroy_plan(p);
    fftw_free(in); fftw_free(out);

    return 0;
}
我的task.json,注意我的头在'/usr/local/include'中,我的库在'/usr/local/lib'中

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-I/usr/local/include",
                "-L/usr/local/lib",
                "-lfftw3 -lm",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Generated task by Debugger"
        }
    ],
    "version": "2.0.0"
}
My c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/lib",
                "/usr/local/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "intelliSenseMode": "clang-x64",
            "cppStandard": "c++20"
        }
    ],
    "version": 4
}
当我试图在我的VSCode中调试它时,我得到以下消息

> Executing task: C/C++: clang++ build active file <

Starting build...
Build finished with error:
Undefined symbols for architecture x86_64: "_fftw_destroy_plan", referenced from: _main in test-eb8667.o
"_fftw_execute", referenced from:_main in test-eb8667.o
"_fftw_free", referenced from:_main in test-eb8667.o
"_fftw_malloc", referenced from:_main in test-eb8667.o
"_fftw_plan_dft_1d", referenced from:_main in test-eb8667.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The terminal process terminated with exit code: -1.

Terminal will be reused by tasks, press any key to close it.
>正在执行任务:C/C++:clang++生成活动文件<
正在开始生成。。。
生成已完成,但出现错误:
架构x86_64的未定义符号:“_fftw_destroy_plan”,参考自:_test-eb8667.o中的main
“_fftw_execute”,引用自:_test-eb8667.o中的main
“\u fftw\u free”,引用自:test-eb8667.o中的\u main
“_fftw_malloc”,引用自:test-eb8667.o中的主要部分
“\u fftw\u平面图\u dft\u 1d”,参考文件:test-eb8667.o中的主管道
ld:找不到架构x86_64的符号
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)
终端进程终止,退出代码为:-1。
终端将被任务重用,请按任意键将其关闭。
但是,我可以在终端中使用相同的命令
g++test.cpp-I/usr/local/include-L/usr/local/lib-lfftw3-lm-o test编译它,并且输出结果是正确的


有人知道为什么会发生这种情况吗?

事实证明,我应该将我的“task.json”类型指定为“shell”,然后一切都开始工作了

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${fileDirname}/test.cpp",
                "-lfftw3",
                "-lm",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}

将-v添加到clang的args中,并查看其调用在VSCode和terminal中的差异
{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${fileDirname}/test.cpp",
                "-lfftw3",
                "-lm",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}