How do I include a static library dependency in an Xcode Template?
Asked Answered
L

2

13

I can include a framework like so in a TemplateInfo.plist file:

<key>Frameworks</key>
            <array>
                <string>QuartzCore</string>
                <string>OpenGLES</string>
                <string>OpenAL</string>
                <string>AudioToolbox</string>
                <string>AVFoundation</string>
                <string>UIKit</string>
                <string>Foundation</string>
                <string>CoreGraphics</string>
            </array>

But I cannot find how to do something similar with static libraries. This would greatly improve my template. Is there such functionality?

Lease answered 8/11, 2011 at 21:7 Comment(2)
Hi, I am looking for the same, did you find a solution?Selves
No, sorry! No luck... :(Lease
E
7

I found a solution.

In templateInfo.plist add key Targets --> SharedSettings

<key>OTHER_LDFLAGS</key>
<string>ObjC -all_load -weak_library /usr/lib/libz.dylib ..</string>

It adds your dylib to debug and run settings.. doesn't work with autocomplete as xcode can do with frameworks, but still lot better than doing it manually

EDIT: expalin

<key>Targets</key>
<array>
    <dict>
        <key>Dependencies</key>
        <array><integer>0</integer></array>
        <key>Frameworks</key>
        <array>
            <string>CoreAudio</string>              
        </array>
        <key>SharedSettings</key>
        <dict>
            <key>OTHER_LDFLAGS</key>
            <string>-ObjC -all_load -weak_library /usr/lib/libz.dylib -weak_library /usr/lib/libstdc++.dylib </string>
        </dict>     
    </dict>

Economically answered 23/4, 2012 at 4:19 Comment(1)
Could you specify what you mean by Targets-->SharedSettings? I tried to get this to work but couldn't...Oaf
O
3

The best way I found for doing this is to create an alias to the /usr/lib directory inside your templates folder. From there, you can access all the libs which are in /usr/lib, even the ones which are aliases themselves.

First, I create templates by editing the .plists in XCode, not by editing the xml representations themselves. So, that's how I will be explaining the steps I took to include static libraries into my template.


1) I have a project template: iPhoneOS.platform / Developer / Library / Xcode / Templates / Project Templates / Application / C4 Application.xctemplate

(In Xcode 4.3 the Project Templates / Application can be found directly in the Xcode.app by right-clicking the bundle and choosing Show Package Contents)

image

The guts of this folder look like this:

image

2) As you can see in the image above, I created an alias for the lib folder (/usr/lib) that contains static libraries and moved the alias into my .xctemplate folder.

image

3) In my TemplateInfo.plist file I specify 2 things: a Dictionary and a Node. I put them inside the Definitions and Nodes of the TemplateInfo.plist

image

First, in the Definitions node I specify a dictionary called: Libs/libalias.dylib

Inside this lib I have 2 strings Group : Libs Path : lib/libalias.dylib

The node looks like this:

image

It is important that the syntax be exactly like this, and most importantly that the name of the Dictionary itself specifies the library you want to import. In this case I am importing the libalias.dylib library.

It is also important that the Path be lib/libalias.dylib because this will point to the alias which points to your /usr/lib folder.

Second, in the Nodes array I specify an item as a string which is called Libs/libalias.dylib (note: the exact same name as the Dictionary I specified in Definitions)

image

This is what the Node Item should look like.

4) Once you have these things set up, you can create a new project with your lib already included.

image

NOTE: because I called my Dictionary Libs/... and specified it's Group as Libs the library I am importing appears in a subfolder called Libs in my Project Navigator.

Oaf answered 26/4, 2012 at 6:35 Comment(4)
i was doing it earlier like this.. but i found adding a flag much easier to template as i need to share my template with my team members, asking everyone to create an alias is difficult.Economically
I share my template with a LOT of people, I give them an installer and it works nicely and very clean. You can also also share over git or SVN. Either way, I think my answer is a pretty good one.Oaf
@C4-Travis - Thank you for sharing the solution! In my iOS project, I need to include .a file and a group of .h files. How do I include the .h files? Because there are just too many of them, and I have multiple static library to include, listing each .h file individually does not seem to be a viable solution. Any suggestions? Is there a way I can just specify a folder and have its content included in the project? - Thanks!Hausa
Hey @xueru, the C4 installer actually lists all of the .h files individually. It's a pain but this is the only way I found that worked... I sometimes dread updating the installer template!Oaf

© 2022 - 2024 — McMap. All rights reserved.