May 13, 2012

Use FFTW in C++

Linux/Unix

in Ubuntu it's very easy, just compile the source, then link the library to the project in project property. I use ellipse to write the program, no problem with that.

Windows

I never succeed in linking the fftw library in Microsoft Visual C++ Express 2010, though my friend told me there's no problem with that. I use 64-bit Windows 7.

there're basically three things you need to do if you have the same environment as me:
all you need is the following: fftw3.h, three .dll files(libfftw3-3.dll,libfftw3l-3.dll,libfftw3f-3.dll), three .def files(libfftw3-3.def,libfftw3l-3.def,libfftw3f-3.def).

1 generate the .lib files

In fact, .lib files is necessary but not included in the .zip files we download from fftw.org. That's because it's better to compile for yourself what you need (-- at least let's think in this way, I know some of you don't agree with me on this point).
open cmd.exe, then direct to the folder where your lib.exe is, for example, here is mine with a default installation of VC++ Express:
 C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin
(oh, wait a moment, you should copy the following files from another folder to the \bin folder before you can proceed, I guess--at least for me it's like this:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE
msobj100.dll
mspdb100.dll
mspdbcore.dll
mspdbsrv.exe
if you are using 2011, I think it's something like msobj110.dll )
 now copy those three .def files into the \bin folder.
then use lib.exe setup under cmd environment to generate the three .lib files in compatible to your system(change your machinetype, 64-bit windows is x64, 32-bit is x86)
lib /machine:x64 /def:libfftw3-3.def
lib /machine:x64 /def:libfftw3f-3.def
lib /machine:x64 /def:libfftw3l-3.def
OK, we're finished with three .lib files. (I recommend you generate another 32-bit like me in case you may use them later) Move the three .lib files out to the original folder where it should be, for example, here I just copy them back to the same folder as  fftw3.h (in fact, you just find a place you find convenient).


2 link the library before you can use #include<fftw3.h> in your code:


copy the three .dll files into the default folder of your project ( the same as .vcxproj and .vcxproj.filters, default for your code). (In fact, I just copy one, because I only use this one: libfftw3-3.dll. And I also have a folder like "\fftw-3.3.1-dll64" here to contain my .h file and .lib files)
open the properties of you project, under the "C/C++" option, chose "general", then add the path of you fftw3.h files(should be in a folder) into "Additional Include Directories"
change to the "Linker" option and choose "general", add the .lib files path(could be the same folder) to the "Additional Library Directories"
under the  "Linker" option and choose "input", add the following items to the "Additional Dependencies":

libfftw3-3.lib
libfftw3l-3.lib
libfftw3f-3.lib

3 (if you're using 64-bit windows as me) change your project to be compiled as 64-bit as you use 64-bit compiled .lib files to the .dll files.
(I get this help from stackflow:
http://stackoverflow.com/questions/1865069/how-to-compile-a-64-bit-application-using-visual-c-2010-express Remind you that you should install Windows7.1SDK if you want to debug as 64-bit with Express 2010, with other Visual Studio I think you have x64 debugger with you )
However, I didn't succeed with that as I mentioned at the beginning. I tried 32-bit .lib files and it didn't work either. As frustrated as me, I installed the trial version of Visual Studio 2011, where I got the x64 debug on its installation and it's easy to get it done like this( almost same as the above link):
Go to Properties of your project. On the top of the dialog box there will be a "Configuration" drop-down menu. Make sure that selects "All Configurations." There will also be a "Platform" drop-down that will read "Win32." Finally on the right there is a "Configuration Manager" button - press it. In the dialog that comes up, find your project, hit the Platform drop-down, select New, then select x64. When you return to the Properties dialog box, the "Platform" drop-down should now read "x64."

(
By the way, if you have problem in debugging. Follow this:
 How to rightly set-up debug:
1) Goto your Project Properties
2) On the left expand "Configuration Properties"
3) Expand "C/C++"
4) On the left, Select "General"
5) On the right, change "Debug Information Format" to "Program Database For Edit And Continue (/ZI)"
5) On the left, Select "Optimization"
6) On the right, change "Optimization" to "Disabled (/Od)"
7) On the left, expand "Linker"
8) On the left, select "Debugging"
9) On the right, change "Generate Debug Info" to "Yes"
10) Click ok
11) Set your breakpoints
12) Rebuild your application
Also when running your application use Ctrl+F5 to build and run it, this keeps the console window open long enough for you to see your output.
)

OK, well done, now you can #include<fftw3.h> and test an example. The best one is on the fftw manual, check it instead of the Internet.

6 comments:

Antonio said...

Hello, I followed your explanation for the creation of libraries, but I get the following error:
LINK:fatal error LNK1104: cannot open file 'libfftw3l-3.lib'

This happens the other .def file


Can you help me?thanks

Xingguang Liu said...

I will give another post which have screenshots for you (and for me if I forget) to set up the configuration correctly.

If you still find problem, we can discuss on that.

BTW, I recommend that you install the Visual Studio 2011. If you don't use WPF, you can install express for free. If you want to use it and you're a student, you can request a serial number from Dreamspark.

Lior Ella said...

Hi Antonio and Xingguang,

I ran into the same problem as Antonio, and after messing around with it for about an hour I figured it out. The problem was that I tried to create the *.lib files in the visual studio directory itself. Because admin privileges are required to add files to that directory, it failed.

The solution is to run lib.exe through the visual studio command prompt, but to do it in a "neutral" directory, where the *.def files are found. This will work.

Thanks a lot!

Russell said...
This comment has been removed by the author.
Unknown said...

Hi Xingguang Liu
I followed your explanation to create .lib and .exp files.But i am getting the following error.

LINK:fatal error LNK1104: cannot open file 'libfftw3l-3.lib.
Please help me out

Unknown said...
This comment has been removed by the author.