71

I have the following import in my code:

@import Foundation;

I am encountering the following build error:

use of @import when modules are disabled

I have seen @import vs #import - iOS 7 and I have set "Enable Modules" to "YES".

Despite this, my problem is not solved.

3
  • 3
    What is your problem? Commented Apr 17, 2015 at 14:28
  • Your question is surely your answer? I thought that you had to enable modules to be able to use @import? Commented Jan 19, 2016 at 9:41
  • Unfortunately @import is not supported yet for .mm files or rather Objective-C++ (I tested with both g++ and clang++ as I really wanted to make this work). Commented Jun 16, 2021 at 16:09

5 Answers 5

108

I got this warning in a zero-swift project whenever I tried to add the @import SafariServices; statement.

Solution: Enable the modules. Go to the Target > Build Settings and set the Enable Modules (C and Objective-C modules) to YES.

I've circled the Build Settings toggle to change.

OR

Note: I haven't verified this potential solution, but probably worthy of consideration if there are side effects caused by this solution.

Rather than enabling modules to entire project, we can enable modules for a specific file which is importing c++ file. Go to build phases -> Compile Sources -> Select the file -> Add compiler flag -fmodules

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! worked for me. PS: you forgot to censor your target name :-)
Thanks @Josh , much appreciated.
To my amazement, this didn't resolve the matter for me. Then after running out of ideas as I was sure THIS IS right, I restarted Xcode (9.2) and then it worked totally fine. Sigh.
Xcode 9.3, struggled with same, did same, and happened to read your note Mr. Snell, quit Xcode and opened, and THAT completed the solution. (bad Xcode dog BAD :-)
Thanks Peter - I've been trying to update parse and parse/ui on an old app to move it to sashido and i was banging my head agains the wall that none of the PF stuff was working!
44

The possible cause is that you use Objective-C++. Then modules get disabled despite the proper build settings.

5 Comments

I, at one point had .mm files in the project. They are no longer there. I double checked - no .mm files in project. Also, i have done everything already described in this thread. Still getting the same error. Modules are enabled on all targets. Foundation is imported. I checked the file types on all source files, they are Objective-C, not Objective-C++. Any suggestions?
I noticed the issue is caused by importing the framework (with @import Foundation) into a .mm file
I am using Obj-C++, and facing this same problem. What should I do?
you should create an Obj-C wrapper that use @import (if you actually need to)
hundred times this! i removed @import Foundation from MyFramework.h and it works now!
28

I've been mixing ObjC, ObjC++, C++, and Metal. Whenever I get the "use of @import when modules are disabled" I try replacing:

@import Name; 

with:

#import "Name/Name.h"

example, replace:

@import Metal;
@import MetalKit;
@import CoreVideo;

with:

#import "Metal/Metal.h"
#import "MetalKit/MetalKit.h"
#import "CoreVideo/CoreVideo.h"

It seems to work.

1 Comment

Thanks! Works for me too by replacing @import GoogleMobileAds; with #import "GoogleMobileAds/GADBannerView.h"
14

Check if you are using #import "ProductName-Swift.h" somewhere in .mm files or any other files other than objc files.

Because if you use this import in cpp files then modules gets disabled automatically.

Comments

4

If you are using objective-c++ (.mm file) and none of solutions above work, you can try the following:

  • Open Xcode Build Settings
  • Targets -> Build Settings -> Apple Clang - Custom Compiler Flags > Other C++ Flags
  • Add -fcxx-modules flag

then build again. The error should be gone.

Credit: https://developers.karte.io/docs/setup-react-native-sdk-v2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.