Can not compile FFTW3 library in vscode but same command works in mac terminal

  c++, visual-studio-code

My first test with FFTW library looks like this.

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

My launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "clang++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ build active file"
        }
    ]
}

My task.json, notice that I have my header in ‘/usr/local/include’ and my library in ‘/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
}

When I tried to debug this in my VSCode, I got this following message

> 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.

However, I can compile it in my terminal using the same command g++ test.cpp -I/usr/local/include -L/usr/local/lib -lfftw3 -lm -o test, and the output results are correct.

Does anyone have an idea why is this happening?

Source: Windows Questions C++

LEAVE A COMMENT