Nan Xiao
Nan Xiao

Reputation: 17467

Why can the same code be compiled successfully through gcc while not g++?

The following test.c can be compiled successfully by using gcc compiler:

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

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

While after changing the name to test.cpp, and compile it with g++ compiler, there is an error:

$ 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;
                    ^

Why can the same code be compiled successfully through gcc while not g++?

P.S. My compiler versions:

$ 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)

Upvotes: 1

Views: 560

Answers (3)

Peter
Peter

Reputation: 36597

The basic reason is that C and C++ support complex numbers in fundamentally different ways, which are code incompatible. So C code which uses complex variables, and operations on them, cannot be used in any version of C++, and vice versa.

In C++ (all versions) complex arithmetic is supported by a templated class named complex, which is defined in header <complex>, and is in namespace std. Since it is a C++ (templated) class in namespace std, which C does not support at all, this type and the C++ header <complex> cannot be used in C.

Standard C, before 1999, did not support complex variables or arithmetic. In C (since 1999), complex arithmetic is supported using a _Complex keyword, and a convenience macro complex (#defined in <complex.h>) which uses it. C++ does not support the _Complex keyword or <complex.h>.

Upvotes: 3

lxyscls
lxyscls

Reputation: 329

$ cat test.cpp
#include <complex>

int main(void) {
    std::complex<double> a = 0;
    return 0;
}

In C++, complex is a template, here is the reference std::complex.

Upvotes: 3

Praburaj
Praburaj

Reputation: 613

For C++ Complex type : http://en.cppreference.com/w/cpp/numeric/complex

For C Complex type: How to work with complex numbers in C?

The Complex.h works differently in C and C++

Upvotes: 1

Related Questions