如何用c++11编译c复数

How to compile c complex numbers with c++11

我正在扩展一个项目,该项目用 c99 编译一些库,用 c++11 编译一个库,并在 c99 库中使用 complex.h 个复数。

我知道这可能是个愚蠢的想法,但这不是我的想法,我需要以某种方式让它发挥作用。

代码不能用 gcc4.9 和 -std=c++11 编译,我完全不知道该怎么做。如何编译此代码段?

#include <complex.h>
#include <cstdio>

int main(int argc, char * argv[]) {
  double _Complex a = 3.0 + 0.0 * _Complex_I;
  //printf("%lf\n", creal(a));
  return 0;
}

报错

In file included from /usr/local/include/c++/7.1.0/complex.h:36:0,
                 from main.cpp:1:
main.cpp: In function 'int main(int, char**)':
main.cpp:5:33: error: unable to find numeric literal operator 'operator""iF'
   double _Complex a = 3.0 + 0.0 _Complex_I;
                                 ^
main.cpp:5:33: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
main.cpp:5:33: error: expression cannot be used as a function
   double _Complex a = 3.0 + 0.0 _Complex_I;
                                 ^
main.cpp:5:19: warning: unused variable 'a' [-Wunused-variable]
   double _Complex a = 3.0 + 0.0 _Complex_I;

g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

-std=gnu++11 似乎可以工作,但是有没有办法让它与 -std=c++11 一起工作?

How to compile c complex numbers with c++11

你不知道。标准 C++ 不支持 <complex.h> C 库 header.

或者:

  • 使用 GCC 语言扩展。
  • 为任何处理 C 复数的东西实现一个包装函数,提供一个不使用 C 复数的接口。在 C 中实现包装器并使用 C++ 中的包装器。
  • 首先不要使用 C 复数 - 请改用 std::complex