无法在vscode中编译FFTW3库,但相同的命令在mac终端中有效



我使用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;
}

我的启动.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"
}
]
}

我的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"
}

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

但是,我可以使用相同的命令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"
}

最新更新