f2c's abs conflicts with <complex> abs

K

Klaas Vantournhout

Hi,

I am using CLAPACK to do lots of matrix operations, but this is done on
complex matrices. There I also need to work with normal complex
operators, I use also the standard complex library. Unfortunately this
one conflicts with some stuff in f2c.h needed to run CLAPACK. Examples
are that f2c.h declares a variable complex that fights with everything
from the standard complex header. Most errors I got rid of by dumping
all the Lapack headers in a separate namespace, but now I still did not
resolve one last problem. And I think it might be basic C++.

Assume the following file

** begin code.cpp **

#include <iostream>
#include <complex>

using namespace std;

// putting lapack in a namespace

namespace lapack {
extern "C" {
#include "f2c.h"
#include "clapack.h"
}
}

int main(void) {

cout << abs(complex<double>(0.,1.)) << endl;

/* here comes more code using lapack::(functions) */

return 1;
}

** end code.cpp **

Compiling this gives an error of the sort

error: no match for ‘operator>=’ in ‘std::complex<double>(0.0, 1.0e+0) >= 0’

because fc2.h contains a line
#define abs(x) ((x) >= 0 ? (x) : -(x))

But abs is also defined in the complex header, but preprocessor
directives win over function calls I think.

So I guess you see my question.
How can I circumvent this problem?

Regards
Klaas
 
V

Victor Bazarov

Klaas said:
I am using CLAPACK to do lots of matrix operations, but this is done
on complex matrices. There I also need to work with normal complex
operators, I use also the standard complex library. Unfortunately
this
one conflicts with some stuff in f2c.h needed to run CLAPACK.
Examples
are that f2c.h declares a variable complex that fights with everything
from the standard complex header. Most errors I got rid of by dumping
all the Lapack headers in a separate namespace, but now I still did
not resolve one last problem. And I think it might be basic C++.

Assume the following file

** begin code.cpp **

#include <iostream>
#include <complex>

using namespace std;

// putting lapack in a namespace

namespace lapack {
extern "C" {
#include "f2c.h"
#include "clapack.h"
}
}

int main(void) {

cout << abs(complex<double>(0.,1.)) << endl;

/* here comes more code using lapack::(functions) */

return 1;
}

** end code.cpp **

Compiling this gives an error of the sort

error: no match for ‘operator>=’ in ‘std::complex<double>(0.0,
1.0e+0) >= 0’

because fc2.h contains a line
#define abs(x) ((x) >= 0 ? (x) : -(x))

But abs is also defined in the complex header, but preprocessor
directives win over function calls I think.

So I guess you see my question.
How can I circumvent this problem?

Right after #include "f2c.h" do

#undef abs

V
 
K

Klaas Vantournhout

Right after #include "f2c.h" do
#undef abs

V

I've been thinking about that indeed, but then just wondered if this
solution doesn't influence the working of lapack, there it is based on
this function.
 
V

Victor Bazarov

Klaas said:
I've been thinking about that indeed, but then just wondered if this
solution doesn't influence the working of lapack, there it is based on
this function.

It's not a function. It's a macro here. If you have doubts, move the
#undef _after_ the inclusion of 'lapack'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top