Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Gradusov__Vitaly
Beginner
368 Views

using both C and C++ complex types in one file

Good day,

I have the following simple question: with Intel C++ compiler (Intel Parallel Studio 2013), is it possible to use both C++ std::complex and C complex number (like double _Complex) in one source file? I didn't manage to do that. I tried to use both c++0x (I need it) and c99 options, but the first ovverides the second and the compiler does not recognize the _Complex type.

The reason for the question is as follows: I want to use the ARPACK package (written in Fortran) in C++. For that, I tried to use the

https://github.com/opencollab/arpack-ng

package. It supports the mixed-language programming, for that one needs the file

./ICB/arpack.hpp

which is a collection of C++ interfaces to ARPACK subroutines. But is uses both C++ and C complex numbers (like in lines 197-198, 205-207), and the code is not compiled.

Thank you in advance.

0 Kudos
9 Replies
Viet_H_Intel
Moderator
368 Views

We have icc for C code and icpc for C++ code. So, it's better to use the correct driver for its language. 

Gradusov__Vitaly
Beginner
368 Views

.Thank you for the answer, I now tried to compile the following simple code:

//============================================

//main.cpp

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

class abc {
        std::complex<double> cpp_complex;
        double _Complex c_complex;
        abc();
        ~abc();
};

int main() {

}

//==========================

using my Intel C++ compiler for Linux (icpc (ICC) 18.0.3 20180410) with both icc and icpc drivers:

icc main.cpp

icpc main.cpp

and in both cases the compilation was successful.

BUT when I try to compile the very same code with my Intel C++ compiler for Windows (Intel(R) C++ Compiler XE on IA-32, version 13.0.1 Package ID: w_ccompxe_2013.1.119) with options

/ZI /nologo /W3 /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Qstd=c99 /Qstd=c++0x /Fp"Debug\complex_problem.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd

it fails with an error

error : expected a ";"    c:\Users\vgradusov\documents\visual studio 2010\Projects\complex_problem\complex_problem\main.cpp    6    1    complex_problem


indicating that it does not recognize the double _Complex type. I don't know if it is possible to choose between the icc and icpc drivers in MS VS project. So I wonder why it happens: do I miss some options, or the compiler version is old, or some other problem? I will be very grateful if you can help with it.

Viet_H_Intel
Moderator
368 Views

13.0 version is too old. Please try 19.0 version.

Gradusov__Vitaly
Beginner
368 Views

OK, I will try 18.0 or 19.0 version of Windows compiler (depending on what is available by the university license). But it will take some time.

Anyway, now I ve found another problem: I also need to use complex literals in the program, so I tried to compile the following code:

//==========================================

//main.cpp

#include <complex.h>
#include <complex>
//#include "arpack.hpp"

class abc {
public:
        std::complex<double> cpp_complex;
        double _Complex c_complex;
        abc();
        ~abc();
};

abc::abc() { };
abc::~abc() {};

int main() {

abc cba;
cba.c_complex = 1.0 + 1.0 * I;

}

//==========================================

I do it with the Linux version of compiler (icpc (ICC) 18.0.3 20180410), and when I use

icpc main.cpp

it compiles successfully. BUT when I try to compile it with

icpc -std=c++17 main.cpp

(OR icc -std=c++17 main.cpp OR icpc -std=c99 -std=c++17 main.cpp OR icc -std=c99 -std=c++17 main.cpp)

I get a warning and an error:

//==========

main.cpp(20): warning #3494: a user-provided literal suffix must begin with "_"
  cba.c_complex = 1.0 + 1.0 * I;
                              ^

main.cpp(20): error: user-defined literal operator not found
  cba.c_complex = 1.0 + 1.0 * I;
                              ^

compilation aborted for main.cpp (code 2)

//==========

and it looks like the compiler bug. I ve read that since c++14 user-defined literals must begin with an underscore, but I guess "I" is a provided by the standard library literal? 

Viet_H_Intel
Moderator
368 Views

It isn't a compiler bug. Language changes overtime. If you were compiling with GNU compiler, I think you would see the same error.

Gradusov__Vitaly
Beginner
368 Views

Well, languages change, but they usually change in a way to be compatible with older versions, that is older code should be compiled. I don't see the complex unit constant to be declared deprecated

https://en.cppreference.com/w/c/numeric/complex

But you are right, it doesnt compile with

g++ -std=c++11 main.cpp

but is compiled with

g++ main.cpp

Gradusov__Vitaly
Beginner
368 Views

OK, now I tried to compile the .cpp file that is listed in post #3 with Intel C++ compiler for Windows, version

Intel(R) C++ Compiler for applications running on IA-32, version 19.0.4 Package ID: w_comp_lib_2019.4.245

with options

//=================

/permissive- /GS /W3 /Zc:wchar_t /ZI /Od /Fd"x64\Debug\vc141.pdb" /fp:precise /D "_MBCS" /Zc:forScope /RTC1 /MDd /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Qstd=c++17 /Qprof-dir "x64\Debug\" /Fp"x64\Debug\complex_problem.pch"

//=================

and it failed again, log:

//=====================

1>------ Build started: Project: complex_problem, Configuration: Debug x64 ------
1>main.cpp
1>C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019\windows\compiler\include\complex.h(34): warning #1224: #warning directive: "The /Qstd=c99 compilation option is required to enable C99 support for C programs"
1>  # warning "The /Qstd=c99 compilation option is required to enable C99 support for C programs"
1>    ^
1>
1>C:\Users\vgradusov\source\repos\complex_problem\complex_problem\main.cpp(6): error : expected a ";"
1>          double _Complex c_complex;
1>                          ^
1>
1>compilation aborted for main.cpp (code 2)
1>Done building project "complex_problem.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

//=====================

Note that, if I turn on C99 option instead of C++17, it just says

icl: : warning #10381: option '/Qstd=c99' is not valid for C++ compilations

and does not compile as well.

Gradusov__Vitaly
Beginner
368 Views

The reason it doesnt work on Windows is that the __STDC_IEC_559_COMPLEX__ is not defined (while on Linux it is) ...

Fowler__Austin
Beginner
368 Views

Is there a way to get VS to compile the following main.c

#include <complex.h>

int main() {

double _Complex z;

return 0;

}

It compiles fine at the command line using icl -Qstd=c99 main.c

But inside Visual Studio I can't find a way to do it... is there a way?