c++ - 为什么同样的代码通过gcc可以编译成功,而g++却不行?

标签 c++ c gcc compiler-errors g++

使用gcc编译器可以编译成功以下test.c:

$ cat test.c
#include <complex.h>

int main(void) {
    double complex a = 0;
    return 0;
}
$ gcc -o test1 test.c
$

而改名为test.cpp,用g++编译器编译后,出现错误:

$ cat test.cpp
#include <complex.h>

int main(void) {
    double complex a = 0;
    return 0;
}

$ g++ -o test2 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:4:20: error: expected initializer before ‘a’
     double complex a = 0;
                    ^

为什么同样的代码通过gcc可以编译成功,而g++却不能?

附言我的编译器版本:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 6.2.1 20160830 (GCC)
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 6.2.1 20160830 (GCC)

最佳答案

基本原因是 C 和 C++ 以根本不同的方式支持复数,这在代码上是不兼容的。因此,使用复杂变量及其操作的 C 代码不能在任何版本的 C++ 中使用,反之亦然。

在 C++(所有版本)中,复数运算由名为 complex 的模板类支持,在 header 中定义 <complex> , 并且在命名空间 std 中.因为它是 namespace std 中的 C++(模板化)类,C 根本不支持,这种类型和 C++ 头文件 <complex>不能在 C 中使用。

1999 年之前的标准 C 不支持复数变量或算术。在 C 中(自 1999 年起),使用 _Complex 支持复数运算。关键字和方便的宏 complex ( #define d in <complex.h> )使用它。 C++ 不支持 _Complex关键字或 <complex.h> .

关于c++ - 为什么同样的代码通过gcc可以编译成功,而g++却不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41436886/

相关文章:

c++ - 链接器无法在命名空间中找到函数定义

c++ - 如何在 Debug模式下通过终端运行可执行 C++?

gcc - 禁止将警告视为错误 (cc1.exe)

c - C中的函数调用需要多少条机器指令?

c++ - 嵌套的大括号括起来的初始化列表

C++:非 const 运算符优先于 const 重载

带有字符数组 : "warning: array subscript has type ' char'"的 C 警告

c - fgets() 读取什么?

c - 反转字节序 C

c++ - 在 gcc 中如何在运行时强制符号解析