如何在 Windows 上使用带有 clang 的 C99 复数?

How do I use C99 complex numbers with clang on Windows?

我已经安装了 Visual Studio 2019,安装时发出叮当声。我可以使用 clang.

成功编译 link 应用程序

然而,当我 #include <complex.h> 时,我没有得到符合标准的复数。

这是一个在包含 <complex.h>.

后不起作用的示例
complex float z = 2.0f + 3.0f * I;

它告诉我 complex 关键字未声明。

error: use of undeclared identifier 'complex'

但是,我可以使用 Microsoft 非标准复数。这是一个完整的程序。

#include <stdio.h>
#include <complex.h>

int main( int argc, char *argv[] ) {
  _Fcomplex z = { 2.0f, 3.0f };

  printf( "z = %f + %f * i\n", crealf( z ), cimagf( z ) ); 
  return 0;
}

我可以用 clang -m64 -o cmplx.exe cmplx.c 编译并 link 它。输出是可预测的

z = 2.000000 + 3.000000 * i

我可以在 Windows 上使用 clang 获得符合标准的复数吗?

编译器和它使用的库是分开的。即使您使用 clangyou're still using Microsoft's non-standard libraries.

进行编译

The Microsoft implementation of the complex.h header defines these types as equivalents for the C99 standard native complex types:

Standard type                                   Microsoft type
float complex or float _Complex                 _Fcomplex
double complex or double _Complex               _Dcomplex
long double complex or long double _Complex     _Lcomplex

您可以使用 typdef 和宏来消除其中的一些问题,as demonstrated in this answer