You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt+FFTW代码在MSVC编译报错,MinGW正常,求解决方案

Qt6 + FFTW在MSVC编译失败的解决方法

可在MinGW64下正常运行的代码

#include <QCoreApplication>
#include "fftw3.h"

int main()
{
    QVector<int> data = {0, 1, 0, 1, 0, 1, 0, 1}; // 2^3=8 values

    //-----Start fft----- (using fftw library)
    fftw_complex in[data.size()], out[data.size()];
    for (int i = 0; i < data.size(); i++) {
        in[i][0] = data[i];
        in[i][1] = 0;
    }
    fftw_plan p;
    p = fftw_plan_dft_1d(data.size(), in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    //-----End fft-----

    double Amplitude;
    for (int i = 0; i < data.size(); i++) {
        Amplitude = sqrt( pow(out[i][0], 2) + pow(out[i][1], 2) );
        qDebug() << Amplitude;
    }
    return 0;
}

MSVC 64位编译器(版本17.4.33213.308)编译错误

error C2131: expression is not defined by a constant
note: the crash was caused by an undefined function or a function not declared to "constexpr"
note: see usage of "QList<int>::size"
error C2131: expression is not defined by a constant
note: the crash was caused by an undefined function or a function not declared to "constexpr"
note: see usage of "QList<int>::size"
error C3863: array type "fftw_complex['function']" is non-assignable
error C3863: array type "fftw_complex['function']" is non-assignable
error C2668: pow: ambiguous call to overloaded function

项目.pro文件内容

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

SOURCES += \
        main.cpp

LIBS += D:\QtProjects\fft\lib\libfftw3-3.lib
INCLUDEPATH += D:\QtProjects\fft\lib

问题分析与解决方案

1. 变长数组(VLA)导致的编译错误

MSVC不支持C99标准中的变长数组扩展,而MinGW允许这种非标准写法。代码中fftw_complex in[data.size()], out[data.size()]使用运行时变量data.size()作为数组长度,这在MSVC中不被允许。

解决方法:使用FFTW提供的动态内存分配函数替代栈上的变长数组:

// 替换原数组定义
fftw_complex* in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * data.size());
fftw_complex* out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * data.size());

// 使用完成后释放内存
fftw_free(in);
fftw_free(out);

2. pow函数的歧义调用

MSVC中pow有多个重载版本,当传入double和int类型参数时,编译器无法确定使用哪个重载。

解决方法:将整数参数改为浮点型,明确指定参数类型:

// 把2改为2.0,匹配double版本的pow
Amplitude = sqrt( pow(out[i][0], 2.0) + pow(out[i][1], 2.0) );

3. FFTW库兼容性确认

确保你使用的libfftw3-3.lib是针对MSVC编译器编译的版本。MinGW编译的库无法在MSVC环境中直接使用,若当前库是MinGW版本,需要下载或自行编译MSVC版本的FFTW库。

修改后的完整代码示例:

#include <QCoreApplication>
#include "fftw3.h"
#include <cmath>

int main()
{
    QVector<int> data = {0, 1, 0, 1, 0, 1, 0, 1}; // 2^3=8 values

    //-----Start fft----- (using fftw library)
    fftw_complex* in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * data.size());
    fftw_complex* out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * data.size());
    
    for (int i = 0; i < data.size(); i++) {
        in[i][0] = data[i];
        in[i][1] = 0;
    }
    
    fftw_plan p = fftw_plan_dft_1d(data.size(), in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    //-----End fft-----

    double Amplitude;
    for (int i = 0; i < data.size(); i++) {
        Amplitude = sqrt( pow(out[i][0], 2.0) + pow(out[i][1], 2.0) );
        qDebug() << Amplitude;
    }
    
    // 释放内存
    fftw_free(in);
    fftw_free(out);
    
    return 0;
}

内容的提问来源于stack exchange,提问作者Anton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 13:35:18