Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for framework's existence at compile time?

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.

like image 292
johngraham Avatar asked Mar 26 '13 16:03

johngraham


2 Answers

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.

like image 73
Greg Parker Avatar answered Nov 15 '22 03:11

Greg Parker


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.

like image 2
ma11hew28 Avatar answered Nov 15 '22 01:11

ma11hew28