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

2.1k Views Asked by At

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?

2

There are 2 best solutions below

1
On

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>

4
On

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.