Linking quadmath with C++

I have an example code:


#include <quadmath.h>
int main()
{
     __float128 foo=123;
     cosq(foo);
     return 0;
}

I tried to compile it with the following commands:


g++ f128.cpp -lquadmath
g++ f128.cpp /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a
g++ f128.cpp /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a /usr/lib64/libquadmath.so.0
g++ f128.cpp /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a /usr/lib64/libquadmath.so.0 /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a

All these commands produce one and the same error:


f128.cpp:(.text+0x1b): undefined reference to `cosq(__float128)'

I also tried to declare cosq as follows, without inluding quadmath.h. Declarations of such style are used in C++ interface to fortran subroutines in other programs, and they work well.


extern "C" __float128 cosq_(__float128 *op);
extern "C" __float128 cosq_(__float128 op);
extern "C" __float128 cosq(__float128 *op);
...and so on...

Result was the same.
Then I tried to use cosq in Fortran:


PROGRAM test
          REAL*16 foo
          REAL*16 res
          foo=1;
          res=cos(foo)
         PRINT *,res
END

This program compiles and executes well (prints the answer with lots of digits), so cosq works in it. This program was compiled with no options: gfortran f128.f90.

I tried OpenSUSE 12.1 with gcc 4.6.2 and OpenSUSE 12.2 with gcc 4.7.1. *.h, *.a and *.so files mentioned are provided by gcc46-fortran and libquadmath46 packages.
All needed include files and libraries exist, linker doesn’t print any nonexistance errors.

What is the proper way to use cosq and other quadmath functions in C++?
I wouldn’t like to write Fortran wrappers for them.

Am 08.12.2012 20:36, schrieb virtual void:
> #include <quadmath.h>
Since that is a C header if I am not wrong you need a extern statement
(not tested)


extern "C" {
#include <quadmath.h>
}

and forget about redefining the prototypes yourself.


PC: oS 12.2 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.5 | GTX 650 Ti
ThinkPad E320: oS 12.2 x86_64 | i3@2.30GHz | 8GB | KDE 4.9.4 | HD 3000
eCAFE 800: oS 11.4 i586 | AMD Geode LX 800@500MHz | 512MB | lamp server

This works fine, thank you!
Program links correctly with -lquadmath.