Author Topic: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)  (Read 381 times)

Offline Angelos_Fra

  • Single posting newcomer
  • *
  • Posts: 5
I want to install the FFTW3 library in my system which runs on Windows 10. My compiler is the minGW GCC 10.1 and I am using Code::Blocks.

I have downloaded the 64bit version from http://www.fftw.org/install/windows.html and unzipped it in C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3.

Then I followed the steps described here https://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/. In step 4 it says "Tell the linker where to look for the library file(s) for the library."

Since there is no .lib file, it had to be created. I copied the libfftw3-3.def file and pasted it inside the bin folder of the mingw64 compiler, so that the dlltool can be used. Note that inside the same bin folder there exists the as.exe executable. I added this directory, namely C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin to system PATH.

Then I opened the cmd window as admin and executed dlltool -v -d nlib32.def -l nlib32.lib. The 2KB libfftw3-3.lib file was created inside the compiler's bin folder.

Then, again, I followed the steps that are described in the link I provided above. For convinience, I am posting the steps directly below.

Once per library:

1) Acquire the library. Download it from the website or via a package manager.
2) Install the library. Unzip it to a directory or install it via a package manager.
3) Tell the compiler where to look for the header file(s) for the library. (Here I set the path to be C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3)
4) Tell the linker where to look for the library file(s) for the library. (Here I set the path to be C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin)

Once per project:

5) Tell the linker which static or import library files to link. (Again the path was set to C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin)
6) #include the library’s header file(s) in your program.
7) Make sure the program know where to find any dynamic libraries being used.

When I execute the following code

Code: [Select]
#include <iostream>
#include <fftw3.h>

int main(){

    int N = 100;
    fftw_complex *in;
    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);

}

I am getting the following error: "Undefined reference to '__imp_fftw_malloc' ".

I don't know what I am doing wrong so that the compiler cannot find the function that I am calling, but I guess that the problem is in step 7, that is in the "Make sure the program know where to find any dynamic libraries being used." step. Note that this is the first time I had to deal with dynamic-link libraries etc so I am still confused.
« Last Edit: July 09, 2020, 11:19:35 am by Angelos_Fra »

Offline Miguel Gimenez

  • Regular
  • ***
  • Posts: 489
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #1 on: July 08, 2020, 05:32:13 pm »
With MinGW you can link with the DLL without using a LIB, just put the DLL name in Project -> Buld options -> Linker settings -> Other linker options and the DLL path in Search directories -> Linker

If the link still fails post a full rebuild log in code tags:

http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

Offline Angelos_Fra

  • Single posting newcomer
  • *
  • Posts: 5
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #2 on: July 09, 2020, 09:42:07 am »
I am running Code::Blocks version 17.12 on
Windows 10. The compiler I use is minGW64
version 10.1

I am trying to compile the following code
Code: [Select]
    #include <fftw3.h>

    int main(){
    int N = 100;
    fftw_complex *in;
    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
}

but It won't compile.

Build log:
Code: [Select]

-------------- Clean: Debug in test (compiler: MINGW64)---------------

Cleaned "test - Debug"

-------------- Build: Debug in test (compiler: MINGW64)---------------

x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\myHeader_Files -I"..\..\..\..\..\..\..\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\eigen-3.3.7" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\armadillo_lib\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\spectra-0.8.1\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -c "C:\Users\User\Documents\SU MASTER'S\Masters thesis\Code\mySource_Codes\Mesh2D.cpp" -o obj\Debug\mySource_Codes\Mesh2D.o
x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\myHeader_Files -I"..\..\..\..\..\..\..\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\eigen-3.3.7" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\armadillo_lib\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\spectra-0.8.1\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -c "C:\Users\User\Documents\SU MASTER'S\Masters thesis\Code\mySource_Codes\V3Lib.cpp" -o obj\Debug\mySource_Codes\V3Lib.o
x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\myHeader_Files -I"..\..\..\..\..\..\..\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\eigen-3.3.7" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\armadillo_lib\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\spectra-0.8.1\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -c "C:\Users\User\Documents\SU MASTER'S\Masters thesis\Code\mySource_Codes\Vector.cpp" -o obj\Debug\mySource_Codes\Vector.o
x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\myHeader_Files -I"..\..\..\..\..\..\..\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\eigen-3.3.7" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\armadillo_lib\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\spectra-0.8.1\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -c "C:\Users\User\Documents\SU MASTER'S\Masters thesis\Code\mySource_Codes\Vector3D.cpp" -o obj\Debug\mySource_Codes\Vector3D.o
x86_64-w64-mingw32-g++.exe -Wall -fexceptions -g -I..\myHeader_Files -I"..\..\..\..\..\..\..\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\eigen-3.3.7" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\armadillo_lib\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\spectra-0.8.1\include" -I"C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -c "C:\Users\User\Documents\SU MASTER'S\Masters thesis\Code\test\main.cpp" -o obj\Debug\test\main.o
x86_64-w64-mingw32-g++.exe -L"..\..\..\..\..\..\..\Program Files (x86)\CodeBlocks\myLibraries\FFTW3" -LC:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin -o bin\Debug\test.exe obj\Debug\mySource_Codes\Mesh2D.o obj\Debug\mySource_Codes\V3Lib.o obj\Debug\mySource_Codes\Vector.o obj\Debug\mySource_Codes\Vector3D.o obj\Debug\test\main.o  libfftw3-3.dll 
C:\Users\User\Documents\SU MASTER'S\Masters thesis\Code\test\main.cpp: In function 'int main()':
C:\Users\User\Documents\SU MASTER'S\Masters thesis\Code\test\main.cpp:65:19: warning: variable 'in' set but not used [-Wunused-but-set-variable]
   65 |     fftw_complex *in;
      |                   ^~
x86_64-w64-mingw32-g++.exe: error: libfftw3-3.dll: No such file or directory
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 1 warning(s) (0 minute(s), 1 second(s))
 



I have downloaded and unzipped the fftw3-3 library inside a folder. Then, I went to

Settings -> Compiler -> Search Directories -> Compiler Tab and set the path to the folder mentioned above containing the fftw3.h header file.

Then, I went to

Project -> Build Options -> Search Directories -> Compiler Tab and added the path to the fftw3 folder (same as above).

Then, I went to

Project -> Build Options -> Linker Setings-> Other Linker Options and wrote "libfftw3-3.dll" (tried with "libfftw3-3" as well)

Finally, I went to Project -> Build Options -> Search Directories -> Linker Tab and added the path to the fftw3 folder (same as above).

As described in my first post, I also tried to link the library using a .lib file. This did not work either. What am I doing wrong?

« Last Edit: July 09, 2020, 09:46:09 am by Angelos_Fra »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2773
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #3 on: July 09, 2020, 10:57:14 am »
using the dll is wrong in any case. You need the .lib or the .a file. Also this files have to be compatible with the compiler....

Offline Angelos_Fra

  • Single posting newcomer
  • *
  • Posts: 5
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #4 on: July 09, 2020, 11:13:37 am »
using the dll is wrong in any case. You need the .lib or the .a file. Also this files have to be compatible with the compiler....

For windows, I have read that I only need the .lib file. How can I make it compatible with the compiler?

As for the .lib file generation. I did the following:

I copied the libfftw3-3.def file and pasted it inside the bin folder of the mingw64 compiler, so that the dlltool can be used. Note that inside the same bin folder there exists the as.exe executable. I added this directory, namely C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin to system PATH.

Then I opened the cmd window as admin and executed dlltool -v -d nlib32.def -l nlib32.lib. The 2KB libfftw3-3.lib file was created inside the compiler's bin folder.

Is that correct?

EDIT: I have edited my first post as well. There is a description of my first attempt.
« Last Edit: July 09, 2020, 11:21:02 am by Angelos_Fra »

Offline Miguel Gimenez

  • Regular
  • ***
  • Posts: 489
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #5 on: July 09, 2020, 12:00:04 pm »
In the compiler search dialog you have the FTW3 folder twice, one absolue and one relative:

Code: [Select]
"C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3"
"..\..\..\..\..\..\..\Program Files (x86)\CodeBlocks\myLibraries\FFTW3"

In the linker search path you have only the relative version. Both versions have spaces, this sometimes confuses the tools. Try copying the dll to another folder (p.e. c:\temp) and update the linker search path.

Offline Angelos_Fra

  • Single posting newcomer
  • *
  • Posts: 5
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #6 on: July 09, 2020, 12:19:23 pm »
In the compiler search dialog you have the FTW3 folder twice, one absolue and one relative:

And should I have them both as absolute and relative paths?


In the linker search path you have only the relative version. Both versions have spaces, this sometimes confuses the tools. Try copying the dll to another folder (p.e. c:\temp) and update the linker search path.

Should I only have it as relative path?

EDIT: I copied the DLL in another folder whose directory does not have any blanks and tried with both relative and absoulte paths. It says "no such file or directory". It won't compile.
« Last Edit: July 09, 2020, 12:28:08 pm by Angelos_Fra »

Offline gd_on

  • Lives here!
  • ****
  • Posts: 568
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #7 on: July 09, 2020, 12:22:33 pm »
Code: [Select]
For windows, I have read that I only need the .lib file.No.
This is true if you use the visual studio compiler (Microsoft) and if you want to use with it a static library.
For MinGW compiler, the dll is sufficient for a dynamic linking (64 bits in your case). If you want to link statically, you need a static library with the .a extension.
A .lib library may work with MinGW, but I don't think it's recommended.
Windows 10 64 bits (20.04), svn C::B (last version or almost!), wxWidgets 3.1.4, Msys2 Compilers 10.1.0, 64 bits (seh, posix : gcc, g++ and gfortran in C:\msys64\mingw64) or 32 bits (sjlj, posix in C:\msys64\mingw32).

Offline Miguel Gimenez

  • Regular
  • ***
  • Posts: 489
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #8 on: July 09, 2020, 06:30:11 pm »
I have just created a test project. The linker search path is not enough, you must put the absolute path of the DLL in Project -> Buld options -> Linker settings -> Other linker options.

In my case I put G:\Librerias81\fftw-3.3.5\libfftw3-3.dll and it compiles OK. You can also define a global variable and write $(#fftw.lib)\libfftw3-3.dll

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

#include <fftw3.h>

int main()
  {
  int N = 100;
  fftw_complex *in;
  in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);
  fftw_free(in);
  return 0;
  }
« Last Edit: July 09, 2020, 06:31:42 pm by Miguel Gimenez »

Offline Angelos_Fra

  • Single posting newcomer
  • *
  • Posts: 5
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #9 on: July 09, 2020, 07:05:25 pm »
I am reporting below what I have done and it finally worked. The following instructions are based in the guide that can be found at https://www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/.

1 ) Download the precompiled library .zip found in http://www.fftw.org/install/windows.html
2 ) Unzip into a new folder named "FFTW3" and place it wherever you like, for example C://myLibraries//FFTW3.
3 ) Copy the libfftw3-3.def file and paste it inside the "bin" folder that can be found inside the folder where your compiler is. For example, in my case C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin
4 ) Put the directory of step 3 to system PATH. You can easily find instructions on web, especialy for windows 10.
5 ) Open the command window and change your directory to the one in step 3. Then execute:
Code: [Select]
dlltool -v -d libfftw3-3.def -l libfftw3-3.a
6 ) Now you can delete the libfftw3-3.def file from the compiler's bin directory and cut the libfftw3-3.a file that was created in there and paste it inside the directory in step 2. I created a folder named "Lib" so, in my case, the directory is C://myLibraries//FFTW3//Lib.
7 ) Open Code::Blocks and go to settings -> compiler -> Search directories ->compiler Tab and add the directory of step 2, namely the location where the header file .h is located.
8 ) Go to Settings -> Compiler -> Search directories -> Linker and add the directory where the .a file is located (see step 6).
9 ) Go to Project -> Build options -> Search directories -> Compiler and add the directory of step 2.
10 ) Go to Project -> Build options -> Search directories -> Linker and add the directory of step 6.
11 ) Go to Project -> Build options -> Linker settings and add the file .a (you have to literally select the file .a, not the folder where it can be found).
12 ) Copy the libfftw3-3.dll that can be found inside the FFTW3 folder and paste it inside the folder where the main.cpp is.

Now the following test code should be working fine.

Code: [Select]
#include <fftw3.h>
int main()
  {
  int N = 100;
  fftw_complex *in;
  in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);
  fftw_free(in);
  return 0;
  }

« Last Edit: July 09, 2020, 07:11:15 pm by Angelos_Fra »

Offline gd_on

  • Lives here!
  • ****
  • Posts: 568
Re: Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)
« Reply #10 on: July 09, 2020, 07:46:30 pm »
Your process certainly works, but it's rather complicated. As you still use the dll, it's not necessary to create with dlltool an interface library .a. Here, the .a file, created by dlltool is not a static library (though the extension is the same). Nevertheless, MinGW can work without this interface file.
You can use directly the dll in linker setting by entering the name libfftw3-3 without any extension and giving the path to this dll in the settings too (For me, a relative path has been OK). MinGW use the dll in that case. You can choose to place the dll (or a copy) in your bin subfolder, where the executable is created or put the dll in a subfolder that your PATH variable knows.
Windows 10 64 bits (20.04), svn C::B (last version or almost!), wxWidgets 3.1.4, Msys2 Compilers 10.1.0, 64 bits (seh, posix : gcc, g++ and gfortran in C:\msys64\mingw64) or 32 bits (sjlj, posix in C:\msys64\mingw32).