Philipp
Philipp

Reputation: 247

How to compile c complex numbers with c++11

I am extending a project which compiles some libraries with c99 and one library with c++11 and uses complex.h complex numbers in the c99 library.

I know this may be a stupid idea but it wasn't mine and I need to make it work somehow.

The code does not compile with gcc4.9 and -std=c++11 and I am quite clueless what to do. How do I make this snippet compile?

#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;
}

Gives the error

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;

with

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

-std=gnu++11 works it seems, but is there a way to make it work with -std=c++11?

Upvotes: 0

Views: 1167

Answers (1)

eerorika
eerorika

Reputation: 238311

How to compile c complex numbers with c++11

You don't. Standard C++ does not support the <complex.h> C library header.

Either:

  • Use the GCC language extensions.
  • Implement a wrapper function for anything that deals with C complex numbers that provides an interface which doesn't use C complex numbers. Implement the wrapper in C and use the wrapper from C++.
  • Don't use C complex numbers in the first place - use std::complex instead.

Upvotes: 1

Related Questions