Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 676
Chris Hill Avatar asked Nov 08 '11 21:11

Chris Hill


People also ask

How do I import a static library?

Link Static Library to your App Right-click on your libStaticLibrary. a choose Show in Finder, copy files and put them to a created new folder which the name “lib” in the root folder of your project. Create new single view application or open your project where you want to use your library.

How do I create a static library in XCode?

Open XCode and start a new project. Under iOS, select Library and “Cocoa Touch Static Library” say it as "staticlibrary". This will create a nice new project for us that builds a . a file.

How do I create a static library in Swift?

Create Static Library Project Open Xcode and select Cocoa Touch Static Library . Give it a name and select Swift as the development language. In our case, we will call it Networking and assume that it will contain the code to communicate with a back end. Press Cmd+N and select Swift file .


2 Answers

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>

like image 109
Kamal Avatar answered Oct 01 '22 04:10

Kamal


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.

like image 36
C4 - Travis Avatar answered Oct 01 '22 06:10

C4 - Travis