【问题标题】:How do I use C99 complex numbers with clang on Windows?如何在 Windows 上将 C99 复数与 clang 一起使用?
【发布时间】:2019-08-03 18:59:42
【问题描述】:

我已经安装了 Visual Studio 2019,它带有 clang。我可以使用clang成功编译和链接应用程序。

但是,当我#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 链接。输出是可以预见的

z = 2.000000 + 3.000000 * i

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

【问题讨论】:

  • _Complex 有效吗? AFAIK 即使您使用 clang,该库仍然来自 VS。而且 VS 并不真正符合 C99。
  • 是的,_Complex “有效”,但我无法创建虚数,因为我从错误消息中收集到的 I 属于 _Fcomplex 类型。
  • 是的,和我想的一样。你运气不好。您无法获得对complex.h 的实际库支持。 mingw 可能是 Windows 上 C 的更好选择... ;)

标签: c windows clang c99


【解决方案1】:

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

complex.h 标头的 Microsoft 实现将这些类型定义为 C99 标准本机复杂类型的等效项:

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 来解决其中的一些问题。

【讨论】:

    猜你喜欢
    • 2017-03-13
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    相关资源
    最近更新 更多