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

E

Erica

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[0] = sin(i / 10.0 * M_PI); /* Real part */
in[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[0], out[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
 
J

John Harrison

Erica said:
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
 
H

Howard

Erica said:
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
 
E

Erica

That did the trick...thanks!!

Howard said:
Erica said:
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);
 
O

Old Wolf

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top