69

I looked into using tgmath.h to deal with the CGFloat typedef float/double mess when dealing with arm64.

This answer has a pretty good description of how to use it, except that it didn't work at all for me. No matter what, my code was still calling the math.h functions.

After spending some time looking at all of the project compiler settings, I found that disabling the "Modules" feature (@import vs #import - iOS 7) makes it all work. More specifically, the option in the project settings is called Enable Modules(C and Objective-C) in the Apple LLVM 5.1 - Language - Modules dropdown.

To see a quick example of this issue, download a project that uses tgmath, such as MBProgressHUD, and see what happens when you enable the modules project setting. The tgmath.h calls get replaced with regular math.h calls.

My question is:

  1. Why do modules prevent tgmath from being imported properly?

  2. Is there a way to get around it and use both tgmath and modules? I would like to still be able to use them.

Community
  • 1
  • 1
Dima
  • 23,484
  • 6
  • 56
  • 83
  • 5
    That looks like a compiler bug to me, and you should report it at bugreporter.apple.com – Martin R Apr 28 '14 at 08:48
  • 1
    Yes, please do, and please let us know the bug number. – Flash Sheridan Apr 28 '14 at 15:07
  • 3
    I figured as much, thanks guys. I submitted a radar issue and also posted it on openradar. Here is the openradar link: http://www.openradar.me/16744288 – Dima Apr 28 '14 at 17:55
  • 6
    A dirty fix for the time being is copy the #undef and #define macro pairs for each thmath function, right after importing thmath.h. I do this in my .pch file to have those helpers accessible throughout the app. – Matej Bukovinski Apr 29 '14 at 14:09
  • 3
    Thank you; an engineer has been investigating the underlying issue, but I’m afraid I can’t give a timeframe for a fix. – Flash Sheridan Apr 29 '14 at 16:49
  • @MatejBukovinski, thanks for the idea. I was considering doing that too, but ultimately decided to just go with the path of least resistance and turn off modules until this is fixed. Also: great work on MBProgressHUD, I use it all the time :) – Dima Apr 29 '14 at 17:13
  • For anyone following this, my bug was closed as a duplicate of another bug 14459641, so I guess at least someone has reported this before. – Dima Apr 30 '14 at 17:44
  • 1
    http://www.openradar.me/15638008 this one – Dmitrii Cooler Jul 01 '14 at 09:05
  • Mine was closed as a duplicate of 14459641 as well. – Anorak Nov 04 '14 at 09:29
  • I wrote an alternative to tgmath that uses Clang's overloadable attribute: https://github.com/simonwhitaker/swmath – Simon Whitaker Dec 10 '14 at 16:45
  • @SimonWhitaker nice addition but it's worth noting that this is pretty much exactly how tgmath works also. The code is almost identical. – Dima Mar 14 '15 at 17:19

2 Answers2

3

I'm not sure what's causing the issue, but as a workaround you could at least disable modules for only the file(s) where you're using tgmath.h:

  • Navigate to the target's Build Phases tab in Xcode.
  • Under the Compile Sources phase, locate the source files.
  • Double-click the source file and type -fno-modules in the Compiler Flags popover to disable Clang modules for that file.

At least this way you would still get the benefits of modules in most of your project. (This is assuming, of course, you don't need tgmath.h in the majority of your source files.)

Adam Sharp
  • 3,618
  • 25
  • 29
  • Thanks for contributing, unfortunately this is a kind of time consuming solution in a project with hundreds of files. – Dima Aug 26 '15 at 19:26
1

It might already be in your math library under the name ctgmath: Link

Nagarjun
  • 6,557
  • 5
  • 33
  • 51
dammkewl
  • 622
  • 5
  • 23