【问题标题】:How to compile c complex numbers with c++11如何用c++11编译c复数
【发布时间】:2017-10-27 11:36:00
【问题描述】:

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

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

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

#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 一起工作?

【问题讨论】:

  • 不应该是double complex a = 3.0 + 0.0 * _Complex_I;吗? (注意*)
  • 正确。我改正了

标签: c++11 complex-numbers


【解决方案1】:

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

你没有。标准 C++ 不支持 &lt;complex.h&gt; C 库头文件。

要么:

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

【讨论】:

  • 这真的很伤心。 -std=gnu++11 会在 Windows 上编译吗?感谢您的选择!
  • @Philipp 你可以试试 Minimalist GNU for Windows。
  • 谢谢,mingw好像支持-std=gnu++11
猜你喜欢
  • 2012-05-08
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多