Forums

Calling FFTW from C++? (x-posted)

Started by Erica September 15, 2004
Hi,

I am currently working on a program using FFTW-> http://www.fftw.org .
My basic C program to compute the 2D Fourier transform of a bunch of
data works fine when I compile it with gcc. However, I would like to
incorporate that code into a larger program that I wrote with C++.
Unfortunately, I cannot get my program to compile with g++. 

I compile it as follows:

g++ ft2d.c -o ft2d -I/usr/local/include -L /usr/local/lib -lfftw3 -lm

The errors I get are:

ft2d.c: In function `int main()':
ft2d.c:13: invalid conversion from `void*' to `double (*)[2]'
ft2d.c:14: invalid conversion from `void*' to `double (*)[2]'

Here is my code:

#include <fftw3.h>
#include <math.h>

int main(void)
{
    fftw_complex *in, *out;
    fftw_plan p;
    int nx = 5;
    int ny = 5;
    int i;

    /* Allocate the input and output arrays, and create the plan */
    in = fftw_malloc(sizeof(fftw_complex) * nx * ny);
    out = fftw_malloc(sizeof(fftw_complex) * nx * ny);
    p = fftw_plan_dft_2d(nx, ny, in, out, FFTW_FORWARD,
FFTW_ESTIMATE);

    /* Now fill the input array with the input data */
    /* We'll simply make a sine wave */
    for (i = 0; i < (nx * ny); i++) {
        in[i][0] = sin(i / 10.0 * M_PI);    /* Real part */
        in[i][1] = 0.0;                          /* Imaginary part */
    }

    /* Actually execute the plan on the input data */
    fftw_execute(p);

    /* Print out the results */
    for (i = 0; i < (nx * ny); i++)
        printf("Coefficient %d: %f%+f*i\n", i, out[i][0], out[i][1]);

    /* Clean up after ourselves */
    fftw_destroy_plan(p);
    fftw_free(in); fftw_free(out);
    return 0;
} 

Eventually I will do something more complex than a sine wave, but you
get the idea.

If anyone knows what the problem is, I would greatly appreciate any
help. This is all that fftw.org has about calling it from C++:
http://fftw.org/faq/section2.html#cplusplus

Thanks in advance!!
--Erica
"Erica" <erica@technodyke.com> wrote in message 
news:346a55df.0409151004.7edf489e@posting.google.com...
> Hi, > > I am currently working on a program using FFTW-> http://www.fftw.org . > My basic C program to compute the 2D Fourier transform of a bunch of > data works fine when I compile it with gcc. However, I would like to > incorporate that code into a larger program that I wrote with C++. > Unfortunately, I cannot get my program to compile with g++. > > I compile it as follows: > > g++ ft2d.c -o ft2d -I/usr/local/include -L /usr/local/lib -lfftw3 -lm > > The errors I get are: > > ft2d.c: In function `int main()': > ft2d.c:13: invalid conversion from `void*' to `double (*)[2]' > ft2d.c:14: invalid conversion from `void*' to `double (*)[2]'
This is one of the differences between C++ and C. In C void* automatically converts to any other pointer, in C++ you must cast.
> > Here is my code: > > #include <fftw3.h> > #include <math.h> > > int main(void) > { > fftw_complex *in, *out; > fftw_plan p; > int nx = 5; > int ny = 5; > int i; > > /* Allocate the input and output arrays, and create the plan */ > in = fftw_malloc(sizeof(fftw_complex) * nx * ny);
in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * nx * ny);
> out = fftw_malloc(sizeof(fftw_complex) * nx * ny);
out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * nx * ny); That should do it. john
"Erica" <erica@technodyke.com> wrote in message
news:346a55df.0409151004.7edf489e@posting.google.com...
> Hi, > > I am currently working on a program using FFTW-> http://www.fftw.org . > My basic C program to compute the 2D Fourier transform of a bunch of > data works fine when I compile it with gcc. However, I would like to > incorporate that code into a larger program that I wrote with C++. > Unfortunately, I cannot get my program to compile with g++. > > I compile it as follows: > > g++ ft2d.c -o ft2d -I/usr/local/include -L /usr/local/lib -lfftw3 -lm > > The errors I get are: > > ft2d.c: In function `int main()': > ft2d.c:13: invalid conversion from `void*' to `double (*)[2]' > ft2d.c:14: invalid conversion from `void*' to `double (*)[2]' >
You don't note in your code what lines 13 and 14 are, but if I'm counting correctly, the errors are on the fftw_malloc lines, right? You probably need to cast the results of those function calls to (fftw_complex *). It appears that the fftw_malloc call returns a void*, and the compiler is complaining about that type not matching your in and out variables' declared type. -Howard
> Here is my code: > > #include <fftw3.h> > #include <math.h> > > int main(void) > { > fftw_complex *in, *out; > fftw_plan p; > int nx = 5; > int ny = 5; > int i; > > /* Allocate the input and output arrays, and create the plan */ > in = fftw_malloc(sizeof(fftw_complex) * nx * ny); > out = fftw_malloc(sizeof(fftw_complex) * nx * ny);
That did the trick...thanks!!

"Howard" <alicebt@hotmail.com> wrote in message news:<DA%1d.594287$Gx4.169205@bgtnsc04-news.ops.worldnet.att.net>...
> "Erica" <erica@technodyke.com> wrote in message > news:346a55df.0409151004.7edf489e@posting.google.com... > > Hi, > > > > I am currently working on a program using FFTW-> http://www.fftw.org . > > My basic C program to compute the 2D Fourier transform of a bunch of > > data works fine when I compile it with gcc. However, I would like to > > incorporate that code into a larger program that I wrote with C++. > > Unfortunately, I cannot get my program to compile with g++. > > > > I compile it as follows: > > > > g++ ft2d.c -o ft2d -I/usr/local/include -L /usr/local/lib -lfftw3 -lm > > > > The errors I get are: > > > > ft2d.c: In function `int main()': > > ft2d.c:13: invalid conversion from `void*' to `double (*)[2]' > > ft2d.c:14: invalid conversion from `void*' to `double (*)[2]' > > > > You don't note in your code what lines 13 and 14 are, but if I'm counting > correctly, the errors are on the fftw_malloc lines, right? You probably > need to cast the results of those function calls to (fftw_complex *). It > appears that the fftw_malloc call returns a void*, and the compiler is > complaining about that type not matching your in and out variables' declared > type. > > -Howard > > > Here is my code: > > > > #include <fftw3.h> > > #include <math.h> > > > > int main(void) > > { > > fftw_complex *in, *out; > > fftw_plan p; > > int nx = 5; > > int ny = 5; > > int i; > > > > /* Allocate the input and output arrays, and create the plan */ > > in = fftw_malloc(sizeof(fftw_complex) * nx * ny); > > out = fftw_malloc(sizeof(fftw_complex) * nx * ny);
erica@technodyke.com (Erica) wrote:
> > My basic C program to compute the 2D Fourier transform of a bunch of > data works fine when I compile it with gcc. However, I would like to > incorporate that code into a larger program that I wrote with C++. > Unfortunately, I cannot get my program to compile with g++. > > I compile it as follows: > > g++ ft2d.c -o ft2d -I/usr/local/include -L /usr/local/lib -lfftw3 -lm > > The errors I get are: > > ft2d.c: In function `int main()': > ft2d.c:13: invalid conversion from `void*' to `double (*)[2]' > ft2d.c:14: invalid conversion from `void*' to `double (*)[2]'
IMHO if you want to mix C and C++ in a project, you should leave your C files as .C files, (and compile them with gcc not g++). If you include the corresponding .H file from a C++ source file do this: extern "C" { #include "ft2d.h" }; Alternatively you could write a wrapper file that uses this, that would avoid having to do this in every other unit that has to include the C header. There's a chapter in the c.l.c++ FAQ about mixing C and C++ in projects, have a read of it and see if there is any more advice.
Well, looks like you need an explicit cast from the void* data type 
here. But that's basic C/C++ language stuff and has nothing to do with 
FFTW.
-- 
Stephan M. Bernsee
http://www.dspdimension.com