6

Please help me! It takes me hours to look up in the internet and I haven't found a solution....

I am trying to use the call lapack function from C++ functions but I failed at the very beginning. Here is my code:

#include "stdafx.h"
#include "targetver.h"
extern "C" {
#include "lapacke.h"
}


int main{}
{
    return 0;
}

I know "lapacke.h" is a C header so I use the extern "C" clause. But while I try to compile this trivial function, I have the following error:

Error   1   error C2146: syntax error : missing ';' before identifier 'lapack_make_complex_float'   c:\users\svd_example1\example2\example2\lapacke.h   89  1   example2
Error   2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\svd_example1\example2\example2\lapacke.h   89  1   example2

Does anyone know what caused these errors?

Thank you very much!

dragosht
  • 3,237
  • 2
  • 23
  • 32
Jason
  • 1,200
  • 1
  • 10
  • 25
  • @redFIVE, sorry for the format, I've changed it... – Jason Jul 20 '14 at 18:22
  • I wouldn't wrap arbitrary header files in `extern "C"` blocks (and it's usually unnecessary anyway, the header author would wrap the declarations as appropriate). – T.C. Jul 20 '14 at 18:28

1 Answers1

9

The relevant portion of the header is:

/* Complex types are structures equivalent to the
* Fortran complex types COMPLEX(4) and COMPLEX(8).
*
* One can also redefine the types with his own types
* for example by including in the code definitions like
*
* #define lapack_complex_float std::complex<float>
* #define lapack_complex_double std::complex<double>
*
* or define these types in the command line:
*
* -Dlapack_complex_float="std::complex<float>"
* -Dlapack_complex_double="std::complex<double>"
*/

/* Complex type (single precision) */
#ifndef lapack_complex_float
#include <complex.h>
#define lapack_complex_float    float _Complex
#endif

/* ... */    

lapack_complex_float lapack_make_complex_float( float re, float im );

This uses C99 _Complex by default, which Visual C++ doesn't support. You can define those macros as suggested to use std::complex instead, which are supported by Visual C++:

#include <complex>
#define lapack_complex_float std::complex<float>
#define lapack_complex_double std::complex<double>
#include "lapacke.h"
T.C.
  • 133,968
  • 17
  • 288
  • 421
  • I tried your solution and it worked very well! I also tried to remove the extern "C" block, it also worked. Then when do I need the extern "C"? – Jason Jul 20 '14 at 20:10
  • @user3689129 Good library writers will include it in the header itself as necessary if that header is expected to be usable by C++ programs. You should only try adding it if you get a linker error without it - in which case you should also file a bug report with the library writer. – T.C. Jul 20 '14 at 20:18