How to correctly install "fftw" library on Windows

Hi there,

I am new to this forum and hope I chose the correct section for this topic.

I am also completely new to C++ and try to get some exemplary codes running to solve the Schrödinger equation with an operator splitting method (e.g. https://www.algorithm-archive.org/contents/split-operator_method/split-operator_method.html).
I use the MinGW g++ compiler and VS Code as an editor. What I need to do to get the script running is to install the "fftw" library from "http://www.fftw.org/install/windows.html". However I have no clue how to properly install a library containing DLL files in MinGW.

If I write #include "fftw3.h" and copy the file "fftw3.h" (together with all others from the .zip file) into the same directory,I get the following error output from VS Code when trying to compile the script:

[Operator Split Example.cpp 2021-01-30 17:37:32.434]
,,Operator Split Example.cpp: In function 'void split_op(Params&, Operators&)':
Operator Split Example.cpp:145:31: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long long unsigned int'} [-Wsign-compare]
for (int i = 0; i < opr.size; ++i) {
~~^~~~~~~~~~
C:\Users\user\AppData\Local\Temp\cc30uFbn.o:Operator Split Example.cpp:(.text+0x185): undefined reference to `__imp_fftw_plan_dft_1d'
C:\Users\user\AppData\Local\Temp\cc30uFbn.o:Operator Split Example.cpp:(.text+0x1a6): undefined reference to `__imp_fftw_execute'
C:\Users\user\AppData\Local\Temp\cc30uFbn.o:Operator Split Example.cpp:(.text+0x1b9): undefined reference to `__imp_fftw_destroy_plan'
collect2.exe: error: ld returned 1 exit status

I also tried to run another similar C++ script based on fftw from "https://github.com/alexschu98/SplitOperator", which produces essentially the same error output. Hence I suspect an error with the library inclusion.

The mistake is probably really dumb, but after hours of searching and trying I just couldn't make it work with the little experience I have yet.
I used and extracted the 64-bit .zip file containing the DLLs from "http://www.fftw.org/install/windows.html".

Thanks in advance,
bielburg
does it have .lib files?
Usually you add the .lib and .h to your project, and the .dll is used by the compiled code at run time.
These issues make me think you tried to add .dll to the project or use them directly.
Are you trying to compile the library itself, or is that done and trying to use it?

this stuff looks old...
Last edited on
It's a linker error, so you need to link with the fftw3 library.
When I built FFTW3 a while back with MinGW, it created a libfftw3.a file, which you need to link to.

When I built fftw, I believe I followed the same page you did -- the "Building FFTW 3 under MinGW" section. But I built it within the MSYS2 environment because I found that to be the least painful way to build most projects that expect *nix environments.
https://www.msys2.org/
Once msys2 is set up, I launch an msys2 terminal, make sure my path variable includes the /bin folder of where I have mingw installed (e.g. ...;/c/mingw/bin).

Once you can invoke make while in an msys2 terminal, you just do,
./configure {options...}
make
and then locate where it produced the .a file(s).

Here is a 'hello world' sort of program I made to test the library, probably mostly copied from some tutorial, I don't remember.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <cmath>

#include <fftw3.h>

double abs(fftw_complex c)
{
    return std::sqrt(c[0]*c[0] + c[1]*c[1]);
}

double bin_freq(size_t n, size_t N, double Fs)
{
    return n * Fs / N;
}

int main()
{
    fftw_complex *in, *out;
    fftw_plan p;

    const size_t N = 512;
    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);

    double Pi = 3.14159265358979323846;
    double Fs = 44100.0;
    double freq = 3000.0;
    double T = 1.0 / Fs;
    for (size_t i = 0; i < N; i++)
    {
        in[i][0] = std::cos(2 * Pi * freq * i * T);
        in[i][1] = 0.0;
    }

    fftw_execute(p); /* repeat as needed */

    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    for (size_t i = 0; i < N; i++)
    {
        std::cout << bin_freq(i, N, Fs) << " Hz : " << abs(out[i]) << std::endl;
    }

    fftw_destroy_plan(p);
    fftw_free(in);
    fftw_free(out);
}


I have {fftw-3.3.7 path}/api as an additional include directory and {fftw-3.3.7 path}/.libs/libfftw3.a as an object I'm linking with.

Note that my method of just trying to call make while under msys2 might not work for all libraries, for example zlib has been a total pain in the ass to compile in my personal experience, so I'm probably doing something wrong. It almost makes me want to just completely switch to Linux for developing various applications...

________________________________________________________

I haven't used VS Code as an IDE/editor. If you're still having trouble, I could probably try to a clean install of fftw3 again and document exactly what I do, but you might want to try troubleshooting yourself more.
Last edited on
Thanks a lot for your response! I had an urgent deadline to meet, hence the late reply.

So in the mean time I installed Visual Studio 2019 and used the "Additional Include Directories" option of it to specify the fftw directory. This way I do not get the same linker error anymore with my scripts. It still produces another error, namely:

1>C:\Users\User\Documents\___Coding\C++\Split Operator - Git\src\SplitOperator1D.cpp(115,36): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data
1>C:\Users\User\Documents\___Coding\C++\Split Operator - Git\src\SplitOperator1D.cpp(119,60): error C3863: array type 'double [opr.8<L_ALIGN>0]' is not assignable
1>C:\Users\User\Documents\___Coding\C++\Split Operator - Git\src\SplitOperator1D.cpp(134,17): warning C4244: 'initializing': conversion from 'float' to 'int', possible loss of data
1>C:\Users\User\Documents\___Coding\C++\Split Operator - Git\src\SplitOperator1D.cpp(174,31): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data
1>C:\Users\User\Documents\___Coding\C++\Split Operator - Git\src\SplitOperator1D.cpp(187,33): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data

But I think this one is not related to the linking anymore?
When I try to build under Visual Studio 2019 with your code, the terminal prints

Build started...
1>------ Build started: Project: Test, Configuration: Debug x64 ------
1>Test-FFT.obj : error LNK2019: unresolved external symbol __imp_fftw_execute referenced in function main
1>Test-FFT.obj : error LNK2019: unresolved external symbol __imp_fftw_plan_dft_1d referenced in function main
1>Test-FFT.obj : error LNK2019: unresolved external symbol __imp_fftw_destroy_plan referenced in function main
1>Test-FFT.obj : error LNK2019: unresolved external symbol __imp_fftw_malloc referenced in function main
1>Test-FFT.obj : error LNK2019: unresolved external symbol __imp_fftw_free referenced in function main
1>C:\Users\User\source\repos\Test\x64\Debug\Test.exe : fatal error LNK1120: 5 unresolved externals
1>Done building project "Test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

where I am uncertain whether it is a linker error or not.
Also I checked that I do not have a libfftw3.a file in the corresponding folder.
So right now I am not sure how to proceed. If I get the library running with VS 2019 I would be fine to use it instead of MiNGW and VS Code. But I am not sure whether it worked. If not I would probably try your suggestion of using the msys2 approach?
I can't look at it just this minute, but if you just need an FFT implementation, you can use something like https://github.com/mborgerding/kissfft which is just two files:
Declarations are in "kiss_fft.h", along with a brief description of the functions you'll need to use.

Code definitions for 1d complex FFTs are in kiss_fft.c.
Last edited on
Sure. I will keep kiss in mind as an alternative. Since I feel like I need to understand libraries better anyway, I will first try to get fftw running.

As soon as I figure out the exact problem, I will update this post or write a more concrete question.

Thanks again!
Registered users can post here. Sign in or register to post.