How come Xcode doesn't automatically link with QuartzCore when the project uses it?
Asked Answered
C

3

5

I am somewhat curious for iOS apps development with Xcode, even though we specifically state:

#import <QuartzCore/QuartzCore.h>

in our ViewController.h, when the project is linked, the QuartzCore library is not automatically linked. Why is that?

Chlodwig answered 27/4, 2012 at 0:36 Comment(1)
+1 for your question...me too faced same problem even after adding QuartzCore framework. Had to google this syntax #import <QuartzCore/QuartzCore.h> for long. Would loved it if autocomplete feature was there for this.Welldefined
G
11

Because importing a header is in no way connected to linking against a library.

You will need to add QuartzCore Framework to the list libraries that your target links against.

enter image description here

Why does Xcode not do that automatically?

Well, the headers you are importing are actually part of that framework. So Xcode would have to scan through all of its frameworks, check their headers and possibly automatically link the connected libraries. That is certainly doable for OS-related frameworks but close to impossible for custom frameworks as long as Xcode does not know their location. Sometimes, Apps do actually not want to link against a library just because they use e.g. an enum defined in a header.

Gravy answered 27/4, 2012 at 0:41 Comment(0)
E
2

Xcode doesn't automatically add any libraries other than the base 3. It doesn't take much to just link. Perhaps a future version will detect, but for now you have to go to Build Phases and Link them.

Excite answered 27/4, 2012 at 0:44 Comment(2)
yeah, given Xcode is so automated to even autocomplete everything you have in your project, not linking even when it is obvious that you need it, is somewhat strange... but maybe it can prevent accidentally linking too much if we have extra #import that we actually didn't useChlodwig
Obvious is relative. Some people might hate quartz or not want to use it (small group, I know), but auto-linking to such a large and graphics intense framework might not be what everyone wants, especially when CG makes up for Quartz.Doublestop
D
2

The point is that Xcode has no way to know that your project should link with Quartz Core Framework.

#import <QuartzCore/QuartzCore.h> is not enough to say that you need Quartz Core Framework. You can write your own library which included a header file named QuartzCore.h and put it under a folder named QuartzCore. If it is in your include files searching path, it is OK to use, unless you also add the official Quartz Core Framework into your project (which leads to conflicts).

There are other examples. When you import a namespace in Visual Studio for a .NET project, it doesn't automatically add any assembly into "Reference" of your project. It's because you can add a third-party assembly as reference which has the same namespace. You also can add a different version of official .NET Framework assembly.

Defoliate answered 27/4, 2012 at 0:51 Comment(2)
Actually, you provide those OS-related imports using greater than/less than signs to identify them as non user headers (which are simply marked using quotes)Gravy
Generally speaking you are right. But it's still possible to pass additional include path to the compiler and make it search for user headers when using <> form. Refer to #1044860.Defoliate

© 2022 - 2024 — McMap. All rights reserved.