使用Visual Studio 2010在C++中使用LAPACK C头文件出现错误

6

请帮我!我在网上查了好几个小时,但还是没有找到解决方法……

我正在尝试使用来自C++函数的调用lapack函数,但我在一开始就失败了。这是我的代码:

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


int main{}
{
    return 0;
}

我知道"lapacke.h"是一个C头文件,所以我使用extern "C"语句。但是当我尝试编译这个简单的函数时,我遇到了以下错误:

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

有人知道是什么导致了这些错误吗?

非常感谢!


@redFIVE,抱歉格式有误,我已经更改了... - Jason
我不会在“extern”块中包装任意头文件(通常也是不必要的,头文件的作者会适当地包装声明)。 - T.C.
1个回答

9
有关部分如下:
/* 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 );

默认情况下,这使用 C99 的 _Complex 特性,但是 Visual C++ 不支持。你可以按照建议定义那些宏来使用 std::complex 来代替,后者得到了 Visual C++ 的支持:

#include <complex>
#define lapack_complex_float std::complex<float>
#define lapack_complex_double std::complex<double>
#include "lapacke.h"

谢谢你的帮助!我会尝试的! - Jason
我尝试了你的解决方案,它非常有效!我还尝试删除了 extern "C" 块,也可以工作。那么我什么时候需要 extern "C"? - Jason
优秀的库编写者将在头文件中包含必要的内容,以便 C++ 程序能够使用它。 如果在没有该内容的情况下出现链接器错误,则应尝试添加它-在这种情况下,您还应向库编写者提交错误报告。 - T.C.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接