在Linux上将libquadmath与C ++链接 [英] Link libquadmath with c++ on linux

查看:174
本文介绍了在Linux上将libquadmath与C ++链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码:

#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)'

我也尝试如下声明cosq,而不涉及quadmath.h.这种样式的声明在C ++接口中用于其他程序中的fortran子例程,并且效果很好.

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...

结果相同.

然后我尝试在Fortran中使用cosq:

Then I tried to use cosq in Fortran:

PROGRAM test

        REAL*16 foo
        REAL*16 res

        foo=1;
        res=cos(foo)
        PRINT *,res
END

此程序可以编译并很好地执行(用很多数字打印答案),因此cosq可以在其中运行.该程序的编译没有选项:gfortran f128.f90.

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是OpenSUSE 12.1,gcc版本是4.6.2.提到的* .h,*.a和* .so文件由gcc46-fortranlibquadmath46软件包提供.

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.

在C ++中使用cosq和其他四边形函数的正确方法是什么?我不想为他们写Fortran包装器.

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

推荐答案

首先,根据Nikos C.的建议,我在另一台计算机上启动了OpenSUSE 12.2 liveCD(具有gcc 4.7.1),但出现了相同的错误.

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的答案表明,该问题与发行版无关,并且解决方案很简单:

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

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

这在我所有的机器上都可以正常工作.可以使用g++ prog.cpp -lquadmath编译程序.

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

这篇关于在Linux上将libquadmath与C ++链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆