3

How do I reference another project which has code I wish to leverage in XCode 4. In particular I'm trying to make use of the NSDate extensions from here.

Some notes:

  • I was assuming I should probably reference rather than trying build a framework
  • I tried copying the existing "Hello World" xcode project file across into my project, however this didn't seem to work
  • Do I need to create a new "Target" based on "coco touch static library" option?
  • Then would I need to Edit the current Product Scheme so that when I build the new target would build
  • What do I need to do on my project side exactly - should going Add Files, and choosing the extensions Xcode Project File be enough?

thanks

Greg
  • 34,042
  • 79
  • 253
  • 454

2 Answers2

2

I was assuming I should probably reference rather than trying build a framework

yes, reference and link with it, unless you need only a bit of it. at this stage, separating the bits you want may be an advanced topic (depends on the lib's layout/depends as well). you should prefer to reference and link because it will normally minimize your maintenance time, especially if you use it in multiple projects.

I tried copying the existing "Hello World" xcode project file across into my project, however this didn't seem to work

you don't create a project, you add the library's xcode project to your app or library, set the lib as a dependency, add the library to your search paths if needed, then link with the library.

Do I need to create a new "Target" based on "coco touch static library" option?

no

Then would I need to Edit the current Product Scheme so that when I build the new target would build

no. you configure it as a dependency. you may need to alter the lib's build settings if there is a major conflict, which the linker or compiler would point out.

What do I need to do on my project side exactly - should going Add Files, and choosing the extensions Xcode Project File be enough?

start with the process outlined above.

justin
  • 104,054
  • 14
  • 179
  • 226
  • @Justin - thanks - I'm not too sure what you mean exactly in Xcode by "you add the library's xcode project to your app or library, set the lib as a dependency, add the library to your search paths if needed, then link with the library" - I'll have a look in xcode now and see if it stands out to me how to do these things – Greg Mar 21 '11 at 05:12
  • @Greg try it, ask google, read, ask specifics when you get stuck – justin Mar 21 '11 at 05:23
  • @Justin - got a bit further after seeing http://stackoverflow.com/questions/5373192/adding-project-as-dependency-in-xcode-4-x doh - Now I can see the library in Target/BuildPhases/TargetDependencies at least and added it here. When I have a "#import "NSDate-Utilities.h"" however it still doesn't recognize this file. Is there supposed to be some prefix before this perhaps? Or do I need to populate items in the other 3 sections of Target/BuildPhases? – Greg Mar 21 '11 at 05:52
  • to fix the issue, add the parent directory of the file the compiler cannot find to your search paths using the build setting `HEADER_SEARCH_PATHS`. this path may be relative, absolute, or a Source Tree (source tree is a semi-advanced option). an illustration of the usage: `HEADER_SEARCH_PATHS = ../LibName/ $(inherited)` you use `$(inherited)` so you do not overwrite existing search paths. Xcode also offers a gui for this. if you use the gui, then you specify 2 entries: `../LibName/` `$(inherited)`. you may specify an entry to be searched recursively by using 2 asterisks: `../LibName/**` – justin Mar 21 '11 at 06:07
  • did get the compile error next to the import dissapearing by adding to the Project => Header Search Paths => "../3rdpartylibrary/** /**". How after this (coincidence?) I'm getting a recurring XCode 4 crash :( There error is here http://codepaste.net/y4kysh. Think it might be the header search path change I made? – Greg Mar 21 '11 at 07:18
  • managed to say "continue" to the crashes and remove the HEADER_SEARCH_PATHS items - doesn't seem to be crashing anymore - perhaps it was the recursion around $(inherited) that caused an issue? actually it does seem the task of simply referencing another project is somewhat non-intuitive no? – Greg Mar 21 '11 at 07:29
  • @Greg that looks like an Xcode issue to me. i've not spent much time with Xc4 yet, due to the number of issues i've encountered. slightly OT: Xc4 has an option where it will try to automatically resolve build dependencies, so you that would remove one of the 3 steps. it's simple, once you are familiar with it, but very obvious/intuitive. as far as the build setting: are you able to build it w/o Xc crashing? Xc should expand the build settings, so you can see what is passed to gcc/clang. if you literally added 2 entries in quotation: `"../3rdpartylibrary/** /**"` (cont) – justin Mar 21 '11 at 09:27
  • (cont) rather than `"../3rdpartylibrary/**" "/**"`, then something in the toolchain *could* get confused, because that's not how it's done. fully written, it should be `"../3rdpartylibrary/**" "/**" $(inherited)`. however, none of the entries need quotes. you can also escape formal argument separators, if you prefer, or just use the ui. if it's still crashing: *maybe* removing all your projects' derived data will help. – justin Mar 21 '11 at 09:33
  • this time I just added in "../3rdpartylibrary/" alone, and this seemed to 1/2 work. That is the #import "NSDate-Utilities.h" line now does not have an error, however when I go to try to use the extensions they don't seem to be there - e.g. NSDate.xxx and try to use say the NSDate class methods in the header file such as "dateYesterday". Anything spring to mind here Justin? – Greg Mar 21 '11 at 10:32
  • "when I go to try to use the extensions they don't seem to be there" -- you'll have to be specific, if you want my help here =) what's the error? what have you written? if the compiler located the header... and that header is `#import`ed... it's probably there. stab in the dark: `NSDate * yesterday = [NSDate dateYesterday];` does that work? – justin Mar 21 '11 at 11:03
  • arrr - actually it is working when I build - what isn't working is the intellisense - when I said it still wasn't working it was based on me using intellisense to check - don't happen to know whether there a last config item required to get intellisense working too? – Greg Mar 21 '11 at 20:44
  • properly defined search paths are required for code completion to work. getting the project to build is all you'll usually need for code completion to work. – justin Mar 21 '11 at 22:23
1

There is no reason to bring in an actually project. Either you can bring in the source files themselves and you could even use the same exact files instead of copying them if you want. However, if you have more than just a few files, and you don't think you will be changing the code much, then creating a static library would probably be the best option.

drewag
  • 93,393
  • 28
  • 139
  • 128
  • @dreway - thanks - being new to XCode does this mean creating the static library as a new target within the extensions project, then building, and somewhere picking up some form of library file (or framework?) that I manually copy over to my project? (i.e. a snapshot) – Greg Mar 21 '11 at 02:12
  • Yes, you would need to create a new target in the extensions project using the "Cocoa Touch Static Library" template. This will create a new file in your "Products" folder with a ".a" extension. Then you should bring the ".a" file and all of the ".h" files into your project. If you want to use a specific class in your code you would then just import the appropriate header. – drewag Mar 21 '11 at 02:20