Swift compiler error: "non-modular header inside framework module"

Objective CFrameworksSwift

Objective C Problem Overview


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

Objective C Solutions


Solution 1 - Objective C

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

Solution 2 - Objective C

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)

Solution 3 - Objective C

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

Solution 4 - Objective C

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

Solution 5 - Objective C

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.

Solution 6 - Objective C

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.

Solution 7 - Objective C

Don't

#import "MyOtherFramework.h"

Do

#import <MyOtherFramework/MyOtherFramework.h>

Solution 8 - Objective C

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.

Solution 9 - Objective C

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.

Solution 10 - Objective C

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

Solution 11 - Objective C

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.

Solution 12 - Objective C

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

Solution 13 - Objective C

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.

Solution 14 - Objective C

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.

Solution 15 - Objective C

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

Solution 16 - Objective C

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.

Solution 17 - Objective C

I tried every possible solution that I found on this thread setting target-> build settings->Allow non-modular includes in Framework Modules switch to YES! etc. etc.

But nothing worked.

I started getting this error when I installed pods by commenting use_frameworks! , because I wanted to have static library for pods.

Reason Swift compiler throwing (on Xcode 13) this error because I was importing private framework header #import "TFLTensorFlowLite.h" in public .h file, so I imported it in .m file as shown in below screenshots and it stopped throwing this error by Swift compiler (note my Framework had both Objective c and swift code)

enter image description here

enter image description here

Solution 18 - Objective C

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.

Solution 19 - Objective C

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

#import <YandexMobileMetrica/YandexMobileMetrica.h>

Solution 20 - Objective C

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.

Solution 21 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephanView Question on Stackoverflow
Solution 1 - Objective CkgreenekView Answer on Stackoverflow
Solution 2 - Objective CambientlightView Answer on Stackoverflow
Solution 3 - Objective CfunrollView Answer on Stackoverflow
Solution 4 - Objective CVlad BurlaciucView Answer on Stackoverflow
Solution 5 - Objective CKing-WizardView Answer on Stackoverflow
Solution 6 - Objective CJimmy DeeView Answer on Stackoverflow
Solution 7 - Objective ChfossliView Answer on Stackoverflow
Solution 8 - Objective CseoView Answer on Stackoverflow
Solution 9 - Objective CStephanView Answer on Stackoverflow
Solution 10 - Objective CMohamed TAIEBView Answer on Stackoverflow
Solution 11 - Objective CiyunaView Answer on Stackoverflow
Solution 12 - Objective CGeorgi BoyadzhievView Answer on Stackoverflow
Solution 13 - Objective CRob SandersView Answer on Stackoverflow
Solution 14 - Objective CHarisView Answer on Stackoverflow
Solution 15 - Objective Cm_katsifarakisView Answer on Stackoverflow
Solution 16 - Objective CDevB2FView Answer on Stackoverflow
Solution 17 - Objective CWaaheedaView Answer on Stackoverflow
Solution 18 - Objective CChris KlinglerView Answer on Stackoverflow
Solution 19 - Objective CYuri KorshevView Answer on Stackoverflow
Solution 20 - Objective CVittal PaiView Answer on Stackoverflow
Solution 21 - Objective Camurray4View Answer on Stackoverflow