Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link libquadmath with c++ on linux

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.

OS is OpenSUSE 12.1, gcc version is 4.6.2. *.h, *.a and *.so files mentioned are provided by gcc46-fortran and libquadmath46 packages.

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

like image 592
Sergey Avatar asked Dec 08 '12 17:12

Sergey


1 Answers

First, according to Nikos C. advise, I boot up OpenSUSE 12.2 liveCD (which has gcc 4.7.1) on another machine, but got the same error.

Then I posted this question to OpenSUSE forums.

Martin_helm's answer shows that the problem is distro-independent and the solution is trivial:

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

This works fine on all my machines. Program can be compiled with g++ prog.cpp -lquadmath.

like image 189
Sergey Avatar answered Oct 11 '22 14:10

Sergey