Home » Ios » tgmath.h doesn't work if modules are enabled

tgmath.h doesn't work if modules are enabled

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

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

Questions:
Answers:

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