Check for framework's existence at compile time?

大兔子大兔子 提交于 2019-12-18 12:14:42

问题


I'm working on an open-source project that can optionally use a closed-source framework. If the closed-source framework is included in the project, there will be additional functionality. But if the framework isn't included in the project, the project should still compile properly.

How do I check at compile-time if the framework is included in the project?

Basically, I want to do something like this:

#ifdef _MY_FRAMEWORK_EXISTS
#import <MyFramework/MyFramework.h>
#endif

I've seen older questions from 2 years ago like this one, but no answer has surfaced so I might be missing something new now.

I DON'T want to check at run-time via NSClassFromString(), because this will fail at compile time when I try to import MyFramework and it doesn't exist.


回答1:


You can check for the presence of a header file using the __has_include language extension. http://clang.llvm.org/docs/LanguageExtensions.html#include-file-checking-macros

However, that only tells you if the header file is installed. It can't tell you if "Link Binary With Libraries" has linked to its framework.




回答2:


I recommend reading the Mac Developer Library : Framework Programming Guide (which includes a section on Weak Linking).

  1. What do you mean by "exists" or "included in the project"? Do you mean added to the "Link Binary With Libraries" build phase (as described by Including Frameworks)? All that does is affect the linking, not the compilation, build phase. To see this, build. Then, search for -framework in the build log of Xcode's Log Navigator.

    So, yes, if you want to affect the compilation of the code you provided, you could manually define the macro _MY_FRAMEWORK_EXISTS.

  2. I don't really understand what you are trying to do. Can you explain what you want at a higher level? Perhaps, there's a better way to go about it.

    "Minimal overhead" is nice, but too much magic might be confusing. For example, Xcode's magic hides what really happens when including a framework.

    I also recommend checking out how the Facebook SDK for iOS works for high & low-level ideas. It might do the kinds of things you want to do.



来源:https://stackoverflow.com/questions/15642067/check-for-frameworks-existence-at-compile-time

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