问题描述
在编译使用几个自定义c ++库的小型Java程序时遇到问题.所有库均以armeabi-v7a为目标,并通过硬浮动支持进行编译(mfloat-abi = hard-mfpu =霓虹灯-mhard-float -D_NDK_MATH_NO_SOFTFP = 1).我还有一些其他的cpp文件,它们是Java/Android项目的一部分;这些也使用上述设置进行编译,我链接到libm_hard.a(而不是libm.a)
I am running into problems when compiling a small Java program that uses several custom c++ libraries. All the libraries are targeting armeabi-v7a and are compiled with hard-float support (mfloat-abi=hard-mfpu=neon -mhard-float -D_NDK_MATH_NO_SOFTFP=1). I have couple of other cpp files that are part of the Java/Android project; those are also compiled with the above settings and I link against libm_hard.a (instead of libm.a)
当我运行程序时,我从cpp文件(不是自定义库)中得到浮动的奇怪行为,类似于.合法的float值(在代码中,通过调试器检查)作为极小的值(例如1.47895e-309)打印在stdout或fstream上.
When I run the program, I am getting strange behavior with floats from the cpp files (not custom libraries), similar to arm cortex a9 cross compiling strange floating point behaviour. Legitimate float values (in the code, checked via the debugger) are printed on stdout or fstream as extremely small values (e.g. 1.47895e-309).
std::ofstream os;
os.open(filename, std::ios::out);
double x = 0.34343;
double y = log(0.1);
os << x << "\t" << y << endl;
os.close();
似乎发生这种情况是因为libc ++库没有针对硬浮点进行编译.我尝试了静态版本和共享版本的libc ++,并且得到了相同的行为.我还通过readelf检查了它们,似乎它们没有针对硬浮点进行编译(没有提到VPF寄存器).我无法找到为NDK中的硬浮动而编译的libc ++版本(静态或共享).
It seems like this happens because libc++ library is not compiled for hard-float; I tried both static and shared version of libc++, and I get the same behavior. I also checked them via readelf and it appears that they are not compiled for hard-float (no mention of VPF registers). I was not able to find a version of libc++ (static nor shared) that's compiled for hard-float in the NDK.
- Android SDK:21(也尝试过23)
- NDK:14.1.3816874
- Android Studio:2.3.1
我无法(轻松地)将上游库从硬浮动更改为软fp;有些专注于重矩阵处理并针对硬浮进行了优化.而且,据我了解,我无法将softfp和hard混合使用...
I can't (easily) change the upstream libraries from hard-float to softfp; some are focused on heavy matrix processing and optimized for hard-float. And, from what I understand, I cannot mix softfp and hard...
除了重新编译libc ++以进行浮点运算外,我还有其他选择吗? (我知道前一段时间Google放弃了对硬abi的支持,该硬abi必须具有相应的libc ++库)
Beyond recompiling libc++ for hard-float, do I have any other options? (I understand a while back Google dropped support for hard abi, which must have had corresponding libc++ library)
添加了示例代码
推荐答案
r12中的NDK已删除了对硬浮动ABI的支持: https://android.googlesource.com/platform/ndk/+/master/docs/HardFloatAbi.md
The hard float ABI support was removed from the NDK in r12: https://android.googlesource.com/platform/ndk/+/master/docs/HardFloatAbi.md
使用libm_hard停止,您应该会很好(不删除它是r15中已修复的疏忽,对不起造成混乱!).
Stop using libm_hard and you should be fine (not removing this was an oversight that is fixed in r15, sorry for the confusion!).
请务必注意,非硬浮点ABI仍使用浮点指令.唯一的区别是传递给ABI的函数参数.
It's really important to note that the non-hard-float-ABI still uses floating point instructions. The only difference is the function argument passing ABI.
这篇关于Android NDK:libc ++对浮点数的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!