Search code examples
rcgcccompilationcomplex-numbers

How do I get gcc to connect type Rcomplex with type double complex?


I have (hopefully) hacked the R package MASS to make it capable of performing robust linear fits on complex number data. It uses external c and FORTRAN code to perform some linear algebra which I've modified / replaced to accommodate complex numbers.

While trying to compile a c source file via R CMD SHLIB, I getting errors such as:

error: invalid operands to binary - (have ‘Rcomplex’ {aka ‘struct <anonymous>’} and ‘Rcomplex’ {aka ‘struct <anonymous>’})

Clearly, the compiler is not understanding that the Rcomplex type should be treated as a double complex type. This isn't completely unexpected, since Writing R Extensions mentions that this can happen for certain compilers with certain configurations. However, I haven't been able to find any information on how to change the configuration so that it does work.

I've tried changing the optimization level to no avail, and including various header files in addition to complex.h, Complex.h, Rmath.h, and R.h has not worked either.

Does anyone know what I could be missing?


Solution

  • If you want to take a chance based on the statement On most platforms this is stored in a way compatible with the C99 double complex type, you can e. g.

    #define Ccomplex(Rcomplex) (*(double complex *)&Rcomplex)
    

    and apply this to each Rcomplex operand, or you can even try to change the definition of Rcomplex to

    typedef double complex Rcomplex;
    

    throughout the C source files. But keep in mind that even if it works on one machine type, you can't be sure that it works on others.