use of @import when modules are disabled
Asked Answered
U

5

70

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.

Unlookedfor answered 17/4, 2015 at 14:12 Comment(3)
What is your problem?Omission
Your question is surely your answer? I thought that you had to enable modules to be able to use @import?Fachini
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).Chalco
K
106

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

Kitchenware answered 6/2, 2017 at 0:49 Comment(5)
Thanks! worked for me. PS: you forgot to censor your target name :-)Thou
Thanks @Thou , much appreciated.Kitchenware
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.Farmann
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 :-)Laundrywoman
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!Lagging
S
42

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

Spanishamerican answered 30/3, 2016 at 11:32 Comment(5)
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?Helainehelali
I noticed the issue is caused by importing the framework (with @import Foundation) into a .mm fileTine
I am using Obj-C++, and facing this same problem. What should I do?Forecourt
you should create an Obj-C wrapper that use @import (if you actually need to)Ensnare
hundred times this! i removed @import Foundation from MyFramework.h and it works now!Flannel
C
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.

Candlestand answered 27/1, 2019 at 10:3 Comment(1)
Thanks! Works for me too by replacing @import GoogleMobileAds; with #import "GoogleMobileAds/GADBannerView.h"Mudd
F
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.

Fateful answered 28/9, 2016 at 17:45 Comment(0)
I
3

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

Incidental answered 25/2, 2023 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.