209
votes

Now I would like to migrate my ObjC framework to Swift and I got the following error:

include of non-modular header inside framework module 'SOGraphDB'

The references is to a header file which just define a protocol and I use this header file in some classes to use this protocol.

Is seems related to the module feature but it is at the moment not quite clear how to fix, do you know a solution?

UPDATE:

This is a Swift compiler error.

UPDATE 2:

A quick fix (but not solving the root cause) is to set the following setting to yes: CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES

20
It seems there is a new build setting for " CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES"Stephan
Has anyone seen this on includes that are public and modular? I see this with a vanilla (cocoapods) project: github.com/CocoaPods/CocoaPods/issues/3092 and dropbox.com/s/trhe5vwhzoa9bf5/…Chris Conover
Has anyone made a quick script that enables this automatically?fatuhoku
@fatuhoku yeahfunroll
None of these solutions worked for me, it looks like it was a bolts.framework collision in my case. Deleting it solved the problem: stackoverflow.com/a/33114309/3324388Aggressor

20 Answers

322
votes

Is your header public?

Select the header file in the project explorer. Then in the section on the right in xcode, you'll notice there is a dropdown next to the target. Change that from "project" to "public". This worked for me.

public header

137
votes

This is an expected compiler behaviour and for a very good reason.

I think the majority of people running into this issues is caused after they switch from Application Target to Framework Target and start adding C and Objective C headers into framework's umbrella header expecting it to have a same behaviour as application's Bridging Header, which behaves differently. The umbrella header is actually designated for mixed swift, obj-c framework and its purpose is exposing the APIs to the outer world that your framework has in objective-c or c. That means the headers we put there should be in the public scope.

It should not be used as a place that exposes Objective-C/C headers that are not a part of your framework to your framework's swift code. Because in that case these headers will be also exposed as the part of our framework module to the outer world, which is often not what we want to do since it breaks the modularity. (And that is exactly why Allows Non-modular Includes in Framework Modules defaults to NO)

In order to expose Objective-C/C library to your framework swift code, we should define a separate swift module for such library. Then a standard swift import YourLegacyLibrary can be used.

Let me demonstrate this on some typical scenario: embedding libxml2 into our framework.

1. You first need to create a module.modulemap file which would look in this way:

For OSX framework:

module SwiftLibXML2 [system] {
  header "/usr/include/libxml2/libxml/xpath.h"
  export *
}

For iOS framework:

module SwiftLibXML2 [system] {
  header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/libxml2/libxml/xpath.h"
  export *
}

All it does is that it wrap ups the header and any other headers it references inside swift module, so that swift will then be able to generate the swift bindings for these C interfaces.

2. Then in your xcode project directory create a folder SwiftLibXML2 and put this module.modulemap there

3. In Build Settings, add $(SDKROOT)/usr/include/libxml2 to Header Search Paths

4. In Build Settings, add $(SRCROOT)/SwiftLibXML2 to Import Paths

5. Under Project's General tab, add libxml2.tbd to Linked Frameworks and Libraries.

Now you import this module where needed with:

import SwiftLibXML2

(if you want to look a more complete module.map example, I would suggest referencing Darwin's module.modulemap at /usr/include/module.modulemap, you would need to have Xcode command-line tools installed to go there, reference Missing /usr/include in OS X El Capitan)

55
votes

Here's how to automatically apply the quick fix so you don't have to change Pods.xcodeproj manually after each pod install.

Add this snippet to the end of your Podfile:

post_install do |installer|
  installer.pods_project.build_configuration_list.build_configurations.each do |configuration|
    configuration.build_settings['CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES'] = 'YES'
  end
end
30
votes

Solution for me was to go on target-> build settings->Allow non-modular includes in Framework Modules switch to YES!

15
votes

I think I got around this. I have some model code that uses sqlite3 in a framework. In my case, the culprit was <sqlite3.h>.

The problem was that in my Module/Module.h header, I imported a public header that imported <sqlite3.h>. The solution was to hide all the sqlite3_xxx types and make sure they were not visible in any public .h. All direct references to sqlite3 were made private or project visibility. For example, I had a public singleton that had some sqlite3_stmt pointers hanging off it. I moved those to a separate class that is now only a forward declaration in that public header. Now I can build.

Incidentally, the CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES setting didn't work. I tried setting it both in the framework and the dependent project. This workaround was necessary, though I'm not sure why.

15
votes

In Swift:

1. Modify your Xcode project and targets' Build Settings as mentioned below:

Allow Non-modular Includes In Framework Modules: No

Enable Bitcode: Yes

2. Use the current latest version available for GoogleMaps iOS SDK (use CocoaPods for getting it):

GoogleMaps (1.10.4)

3. Comment the problematic import:

//import GoogleMaps

4. Create or modify your bridging header file, adding the problematic import:

[Your Xcode Project Name]-Bridging-Header.h

// Use this file to import your target's public headers 
// that you would like to expose to Swift.
#import <GoogleMaps/GoogleMaps.h>

5. Clean and re-build your Xcode project.

7
votes

Don't

#import "MyOtherFramework.h"

Do

#import <MyOtherFramework/MyOtherFramework.h>
6
votes

This answer is out-dated.

When importing frameworks, you must import all header files that share dependencies with the root header. The easiest way to ensure this always works is to import all headers in the framework's "Headers" folder into your public headers path.

enter image description here

The Swift compiler uses this information to generate a map of non-mangled symbols along with their associated type information.

3
votes

The header file was allocated to the target but was only marked as project visible, just a change to public lead to the resolution of this error.

3
votes

Switching Build settings > Allow non-modular includes in Framework Modules to YES! solved the same issue for me.

2
votes

I know that this is an old question, but I had the same issue and nothing from above helped me. So I hope my answer will be helpful for somebody. In my case the problem was in ALWAYS_SEARCH_USER_PATHS setting. When it was set to NO project built and worked ok. But as far as one of the pod required it to be set to YES I was receiving an error

Include of non-modular header inside framework module

After couple cups of coffee and all day researching I found out that according to known issues of Xcode 7.1 Beta 2 release notes:

• If you get an error stating "Include of non-modular header inside framework module" for a framework that previously compiled, make sure the "Always Search User Paths" build setting is set to "No". The default is "Yes" only for legacy reasons. (22784786)

I was using XCode 7.3 though, but seems like this bug hasn't been fixed yet.

2
votes

I would like to add my experience with the problem as well.

Just to summarize :

  • @ambientlight's answer is great and it fixes most of the problems.
  • allowing non-modular headers is another solution (see some of the answers above).
  • marking the framework's headers as public (only the ones that you want to expose) and importing them in the umbrella header.

Here are my 2 additions to the above answer(s):

  • carefully check the imports in your project for headers which import your frameworks directly in them (instead of using forward declaration, if possible) -- it is not a good practice to include a header file inside another header file; sometimes this causes a problems, because if not done properly this may lead to multiple include-ing of one header and create linker issues.
  • UPDATE: make sure that the architectures of the library and that of the target that you want to link it to match.
  • and lastly, after doing all of the above, I still kept bumping onto that error. So I dug a little more and found (in the apple developer forums, but I lost the link :( ) that if you include the headers in the umbrella header not like this <framework/headerName.h>, but only like this "headerName.h", the problem goes away.

I tried this last one, and so far I haven't experienced this problem anymore, however I suspect that this solution is valid only if you have applied some of the top answers (note: they are not all compatible with each other, for example, the module approach and the allowing of non-modular header includes).

1
votes

I had this exact problem when including my own framework in a project. Fixed it by putting all imports of sqlite3.h in .m files not in public .h ones. I'm assuming that other libraries may flag similar issues with Xcode.

1
votes

I had the specific problem with Facebook 4.02 sdk and FBSDKCoreKit.

I did all the steps but still error about non modular header. i drag and dropped only the specific header from the framework to build phases-> header section.

Then automatically created a copy of the header on the project navigator at the top.

I removed it from the build phases -> header and deleted the new file and worked fine.

Like it reseted or something.

1
votes

In my case (Xcode 9 beta 6 - Swift 4 - using Cocoapods) this was solved when I deleted Podfile.lock and the Pods directory and ran pod install again

1
votes

I got this problem after updating a project from swift2 to swift3. I was using XCode 8.3.2 to update the code and could not get rid of the error “non-modular header inside framework module”. When I opened the same project in another version of XCode (version 9.0.1) the error did not appear.

0
votes

Most commonly this error is caused by the chosen answer, yet I had this error appear once by accident when dragging framework files into my new project folder. I clicked to delete the frameworks but accidentally pressed to only 'Remove Reference' to the frameworks rather than to actually delete the files completely. At this point if I opened my project folder in Finder I saw files like 'CoreLocation' and 'AudioToolbox' there. Deleting these files from the project folder and cleaning the project fixed the issue.

0
votes

After allowing to import non modular includes, you could try to import that module using Objective-C Bridging header:

#import <YandexMobileMetrica/YandexMobileMetrica.h>
0
votes

I solved it removing Modules folder from the framework.

  • Browse to your framework location which is present in the App Project using finder

  • Go inside Test.framework folder (In the above case it will be SOGraphDB.framework) & Delete Modules folder.

  • Clean and Re Build the app, it will solve the problem.

-2
votes

I had this problem importing the Parse framework. The only way I could fix it was to discard all my changes since my last commit (simply deleting the framework and cleaning the project did not work) and add Parse again (after a fresh download of the SDK) with its other required frameworks.