Search code examples
xcodexcode4sqlitexcode4.2dylib

Including libsqlite3 dylib in Xcode 4 project template


I have created an Xcode 4 project template for iOS that relies on several frameworks and the libsqlite3.dylib. I have been able to get the frameworks added automatically but can't seem to figure out how to add a dylib. Has anyone had any luck with this?

Edit:
I suppose I wasn't clear enough, I have created my own .xcodetemplate file that appears as a project template when I create a new project. I need to know what to place in the TemplateInfo.plist file to have the libsqlite3.dylib file included in new projects created from the template. I have been successfully able to add .framework files such as CoreMotion by listing them under the Targets->Item 0->Frameworks key, but this doesn't work for dylibs.


Solution

  • I found a way to do this through the Other Linker Flags project settings. To link the sqlite3.0.dylib framework, just add a node to your TemplateInfo.plist under Project->SharedSettings with the Key "OTHER_LDFLAGS" and the value "-lsqlite3.0".

    Note that this will override all of the "Other Linker Flags" for your project template, which many people use for flags such as -ObjC; so, if you want to keep those flags as well just make a list of all flags you want with a space in between them. IE: if you want your project template to include the ObjC linker flag and sqlite, just set the value of the node to "-ObjC -lsqlite3.0".

    For those of you curious how I figured it out, I added the sqlite3.0.dylib framework to my project manually using the Build Phases tab of my target, and then built the project. I then looked at the details of the build by clicking on the log navigator tab in Xcode. I then clicked on the most recent build to see its details, and expanded the "link" section of the build details by clicking on the detail disclosure button at the very right of the text. There I saw a a list of all the libraries and frameworks that it was linking against:

        /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2 -arch i386 -isysroot 
    /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk 
    -L/Users/me/Library/Developer/Xcode/DerivedData/myapp
    -fkymetsrapomrohkudesnxrchwdq/Build/Products/Debug-iphonesimulator 
    -F/Users/me/Library/Developer/Xcode/DerivedData/myapp
    -fkymetsrapomrohkudesnxrchwdq/Build/Products/Debug-iphonesimulator 
    -F/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/Developer/Library -filelist /Users/me/Library/Developer/Xcode/DerivedData/myapp
    -fkymetsrapomrohkudesnxrchwdq/Build/Intermediates/myapp.build/Debug
    -iphonesimulator/myapp.build/Objects-normal/i386/myapp.LinkFileList 
    -mmacosx-version-min=10.6 
    -Xlinker -objc_abi_version 
    -Xlinker 2 -ObjC -D__IPHONE_OS_VERSION_MIN_REQUIRED=40000 
    -framework UIKit -framework Foundation -framework CoreGraphics -framework CoreLocation -framework CFNetwork -framework MediaPlayer -framework MessageUI -framework MobileCoreServices -framework QuartzCore -framework SystemConfiguration -lsqlite3.0 
    -o /Users/me/Library/Developer/Xcode/DerivedData/myapp-fkymetsrapomrohkudesnxrchwdq/Build/Products/Debug-iphonesimulator/myapp.app/myapp
    

    In the list of frameworks I found "-lsqlite3.0" as one of the linked libraries. Adding "lsqlite3.0" to the other linked libraries basically accomplishes the same thing. If there are any other dylib frameworks you want to add to your template, just follow this same process: Add them to your target's build phases, check the build script to see what the library is actually called at link time, and then add them to your list of OTHER_LDFLAGS in your TemplateInfo.plist.

    NOTE: Adding the libraries in this way will not add the frameworks to the build phases UI in XCode, but it will still link it correctly.