iOS CocoaPods - how to resolve “use of '@import' when modules are disabled” error?

若如初见. 提交于 2019-12-04 03:20:02

问题


I'm seeking how to resolve "use of '@import' when modules are disabled" when adding Google Analytics through cocoapods:

pod 'Google/Analytics', '~> 1.0.0'
link_with 'AppTarget', 'AppTargetTests'

In code:

#import <Google/Analytics.h>

I get this error within a pod:

"use of '@import' when modules are disabled"

I checked all projects and see that modules are enabled, along with the other suggestiont to "Link frameworks automatically"

I cannot override this import because of this error:

How can I resolve "use of '@import' when modules are disabled" when adding frameworks through cocoapods in xCode6?


回答1:


I googled a lot, but hacked the solution for your problem myself. Cleaning the project, re-building etc. wasn't working for me.

The solution is to wrap API into Cocoa Class, and use this class in your imports instead of original.

  1. Create class, for example APAnalyticsTracker, where AP supposed to be your common application prefix. Here you'll have two files: APAnalyticsTracker.m and APAnalyticsTracker.h
  2. Import #import <Google/Analytics.h> in APAnalyticsTracker and wrap the original implementation like this (see Gist for more information): https://gist.github.com/vladignatyev/c240a1a4867b17894b10
  3. Use APAnalyticsTracker.h from .mm files freely.
  4. Remember to enable modules, see https://stackoverflow.com/a/33125158/882187 comment from @barrast



回答2:


Just to add a reference to @Alex Stone's answer-in-a-comment, here is the current official clang specification on the lack of support for @import in C++:

At present, there is no C or C++ syntax for import declarations. Clang will track the modules proposal in the C++ committee.




回答3:


In Xcode, just set Enable Modules (C and Objective-C) to Yes in Build Settings:



来源:https://stackoverflow.com/questions/32314151/ios-cocoapods-how-to-resolve-use-of-import-when-modules-are-disabled-erro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!