What do I need to do to use tgmath on iOS?

2019-07-18 04:24发布

问题:

I'm compiling my first project with 64 bit support enabled. I'm running into a bunch of compiler warnings about implicit conversions to float. This is happening because I'm using fabsf() and assigning the result to a CGFloat (which is a double, not float on the new 64 bit architecture).

According to the answer on this question:

CGFloat-based math functions?

I just need to #include <tgmath.h> to solve this problem and probably change fabsf to fabs. I have at least one file where this doesn't seem to be helping. I still get the warning: implicit conversion loses floating-point precision 'double' to 'CGFloat' aka (float). Here's the line that generates that warning:

CGFloat deltaX = fabs(item.center.x-point.x);

Has anyone else run across this? How did you solve it? I'd rather not disable this warning or litter my code with a ton of typecasts.

回答1:

I guess, you are using CGPoint types, so the conversion doesn't occur within fabs(DOUBLE -> FLOAT), but on assignment CGFloat = DOUBLE. That's probably because compiler used fabs from math.h which operates on doubles.

Problem with math.h is that it's internally imported by OSX headers (carbon if I remember correctly), so I guess some iOS header might also do that. After quick look, it seems that basic set of frameworks doesn't import math.h, so probably you should look for it being manually imported. In case it's imported internally by some system libs, you'll probably won't be able to use those libs and tgmath in a single implementation file.

If you want to check if there are some math.h dependencies, you can use a dirty trick to prevent it's inclusion - add this line to a file (or better on top of prefix file):

#define __MATH_H__


回答2:

I was able to get the tgmath.h functions to work by including the header at the top of my PCH file.

At some point (read: Xcode update) I had to start disabling Modules to get this to work. The details of such are in the question Dima links to below.



标签: ios arm64 tgmath