Create XCode 4 lib out of an project - xcode

I have a project with several classes. Now I want two libraries, one exactly for one class and one containing two classes.
Therefore i added two new products "libA.a" and "libB.a"
At the class-m-files i chose as targets "libA.a" or "libB.a"
It tells me, it puts the libs in a folder. But this folder is empty.
Can anyone tell me a step by step explanation, how this is possible.
(I also tried to crate a new product for static libraries, only added this one class and it compiles, but I can nowhere find this lib-File).
So the basic question: Where can I find a Tutorial, how to create a lib (only for IPhone, not for a simulator or universal or so...). URL, ... any help is appreciated.
Or can anyone tell me my missing step(s), do I need to do anything else?

Well, the solution was sooo simple ...
When starting the compilation, it is possible to choose the target next to the play-stop-buttons. The toolbar looks like this.
PLAY STOP TARGET DEVIVE
Click on the Target, chhose LibA, press "Play" (=Compile) and it works. Same for LibB.

Related

How do you create a basic TemplateInfo.plist file with Ancestors and specific frameworks?

Whenever I create a new project I add my own class files -often the same ones. It has become tedious to manually create these over and over. I want an automatic way to do this. I discovered custom Project Templates and I'm not sure if they are well documented by Apple.
I can create a cocos2D project with my custom classes and it works wonders. The problem is when I create a basic empty application with my custom classes.
Basically okay I'll give a little context. See my TemplateInfo.plist: This works. Note the "Ancestors" part. Here's how I interpret it: this inherits from cocos2d templates and does things like:
add necessary frameworks
adds cocos2D things like the splash image
other project-specific things
Now I don't know where these ancestor templates are or what format they are in, so I can't yet break down the individual things and say "OK I want the frameworks they include but not the launch image," but that's essentially what I want to do. I want to start with the most basic template and build from there manually.
I discovered that I can add specific frameworks in the "Targets" area. But before tackling that, I have a question: What Ancestors do I use, so that it does not use the Cocos2D base templates? I want the most basic thing.
1) Try removing all Ancestors and create a new Project and Xcode won't let you name the project and it crashes.
2) Try including only the Ancestors for the Default Empty Application: com.apple.dt.unit.cocoaTouchApplication
And it adds the correct frameworks:
Foundation
CoreGraphics
UIKit
XCTest
which is cool. However I noticed it overrides my Prefix.pch with a [Project-Name]-Prefix.pch and my project does not compile because I include a Debug Logger statement in my custom Prefix.ch like this:
#ifdef DEBUG
#define DLog(...) NSLog(__VA_ARGS__)
I figure this is the "com.apple.dt.unit.cocoaTouchApplication" ancestor doing its thing, and I want custom behavior that's different.
Is there a list of Ancestors somewhere that I can look at? Or is there a way I can look at the specific settings defined in "com.apple.dt.unit.cocoaTouchApplication" (if that even makes sense)?
Has anyone else run into this? What techniques do you use? Does anyone wish for an easier way to do this? Or is it better to create files manually every time?
I'm spinning wheels and any insight would be helpful. Thanks.
I figured it out. Searching through Xcode's default templates, I found one that uses basic ancestors. I used the following two:
And in Targets you can easily add the frameworks you want to include:
There are many little quirks like how to include an Info.plist file and Prefix.pch file and how to get it to rename properly (and replace the default one placed by the template). I am creating a video tutorial to show all these things in detail.
Creating your own custom templates is quite satisfying. Saves a lot of time and hassle when beginning new projects.

How to organize C source file previously compiled by GCC Make and build them into an Xcode bundle? I have a Duplicate Symbol _main Error

How to build a .bundle from source code?
This might sound like a simple problem but it has been hurdling me for a week...
Here is my problem:
I have a bunch of .c and .h files that are organized in a folder and its sub folders. The source code was written and compiled with gcc make and tested by many other make tools. The source code has some utilities and command line tools and it has more code that serve as library for those utilities and tools. It is the files that serve as libraries that I want to reuse. (By library I don't mean static library or something, I just mean that some .c and .h files in certain subfolders provide functions that can be called by some other .c files. I want to be able to call those functions, too)
Yet my problem is more complex than that: I need to build those .c and .h into a bundle to reuse it. I am not writing my application in C; I am developing in Unity and Unity can only take in .bundle files on Mac OS.
Here is my goal:
Organize the source code folder in a proper way so that I can build them into a bundle in Xcode 4.
Here is where I got stuck:
When building the project I got the following error:
Duplicate symbol _main in
/Users/zeningqu/Library/Developer/Xcode/DerivedData/ccn-cfygrtkrshubpofnfxalwimtyniq/Build/Intermediates/ccn.build/Debug/ccn.build/Objects-normal/i386/ccndsmoketest.o
and
/Users/zeningqu/Library/Developer/Xcode/DerivedData/ccn-cfygrtkrshubpofnfxalwimtyniq/Build/Intermediates/ccn.build/Debug/ccn.build/Objects-normal/i386/ccnd_main.o
for architecture i386
I can relate to this error because I can find lots of main entries in the source code. Most of them are test utilities.
Here is what I tried:
I tried removing all those utility .c files but with no luck. The error is still there. I delete and delete until some files cannot find the definition of the function they are calling. So I had to stop there.
Though I wasn't able to build a bundle I was able to build a C/C++ static library (with an .a extension). After I got the .a file I tried to put it into another Xcode project and tried to build it into a bundle. I could build a bundle in that way, but then I had problem accessing the content of the bundle. How do I call functions defined in a .a static library if that library is hidden in a bundle? I read about Apple's documentation which says:
Note: Some Xcode targets (such as shell tools and static libraries) do
not result in the creation of a bundle or package. This is normal and
there is no need to create bundles specifically for these target
types. The resulting binaries generated for those targets are intended
to be used as is.
(quoted from: https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/AboutBundles/AboutBundles.html#//apple_ref/doc/uid/10000123i-CH100-SW1)
Here is what I thought about:
I thought about replacing all main with something like main_sth. But the source code was not written by me so I didn't want to modify it. (It just doesn't feel like a proper way of doing things to me...)
I learnt that Xcode has gcc compiler built in. So I guess if gcc can make it, so can Xcode? It's just a wild guess - I am not familiar with Xcode and gcc.
Here is a summary of my questions:
Is there a way to properly organize a pile of code previously compiled and made by gcc make so that they can be built into an Xcode bundle?
Is it meaningful to put a .a library in an Xcode project and build it into a bundle? If it is meaningful, how do I call functions defined in .a after it is built into a bundle?
Is it proper to just replace all main() entries with something else?
Alright I think I have figured out at least one solution to the problem.
The duplicate main error was caused by a bunch of main entries in my source code. When the code was compiled by gcc make, I guess the author defined a sort of compilation order so that duplicate mains won't be an issue. (If you know how to do this, please let me know. I barely know make tools.) But when I just add the entire source code folder into my Xcode project, of course Xcode would complain during linking...
As I was unwilling to modify the source code (because the source code library is not developed by me), I decided to use another strategy to walk around this problem.
If your duplicate main error was reported from your own code, you can stop reading here. But if you are like me, with a bunch of gcc compiled source code and badly need a bundle yet don't know what to do, I may be able to help.
Okay here is what I did:
I set up an empty workspace.
I built a C/C++ static library project.
Import my entire source code folder into the static library project.
Set some header search path for the static library project.
Build the static library project. (Now I have a .a library which I could link against)
I set up another project, with a bundle target.
At the bundle project -> Build Phases -> Link Binary with Libraries, add the .a library that I just built.
At the bundle project -> edit scheme -> Build, add the static library project to the scheme and move it up the list so that it is built prior to my bundle project.
Then add .h files of my library project to my bundle project as references.
After that, add a .c file in my bundle project that basically functions as a wrapper. I picked a function that I want to call in Unity, wrote a wrapper function in the new .c file, and was able to build the bundle.
After several trial and error, I was able to import the bundle into Unity and was able to call the test function from Unity.
I was really excited about this! Though it's not completed yet I think this gives me hope and I am confident I can use the source code now! And the best thing about this solution is that I don't have to modify the library code developed by others. Whenever they update their code, I just update my .a library and that's it!
Though I have listed 11 steps I still feel that there are lots of details that I missed. So here are my references:
I followed this tutorial to build my source code into a static library: http://www.ccnx.org/?post_type=incsub_wiki&p=1315
I followed this blog to link static library against my bundle code and twist build phases and search headers: http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/
I followed this doc to import my bundle to Unity3D Pro as a plugin: http://unity3d.com/support/documentation/Manual/Plugins.html
I strongly recommend the second reference because that's what solved my problem!
Though the problem is almost solved there are still a few things that I haven't figured out:
I don't know if a wrapper function is at all necessary. I will try this out tomorrow and come back to update.
-- I am coming back to update: the wrapper function is NOT necessary. Just make sure you have all the headers in your bundle project and you will be able to use all the data structures and call functions defined in your headers.
I haven't used NSBundle class though I read a few docs about it. Previously I was thinking about using that class to access my .a library encapsulated in my bundle, but as I found the solution I wrote above, I didn't try the class out.
Lastly, if you have better solution, please don't hesitate to let me know!
I tried to follow the steps in the accepted answer, but had no luck. In the end, I realised step 10 needed to be modified slightly:
Create a dummy.c under (.bundle) project and the dummy.c can just be totally empty.
Remove the setting for the library you want to link inside Link Binary With Libraries
Instead use -Wl,-force_load,$(CONFIGURATION_BUILD_DIR)/libYourLib.a or -all_load to Other Linker Flags
PS: And also can use sub-project instead of workspace. and use Target Dependencies instead of Edit Scheme to achieve the same effect.

Adding OCHamcrest to an IOS Project

The documentation for the project says just add the framework and the linker flags and you are good to go. Hours and hours of wasted time later, I have figured out that that's not true. If you do that, the project does not see the header files. You have to put the framework somewhere were the compiler will find the headers. In my case, that worked when I dropped the framework into /Developer/Library/Frameworks and then told it to recurse in searching that framework directory (do not fiddle around with the headers search directories).
Then the problem I get is that the link fails with the message:
ld: framework not found OCHamcrestIOS
I noticed that the documentation for the project says that it was updated for Xcode 4. I pulled down the binary of the framework after checking out the code and wasting a ton of time unable to build the IOS version of the framework.
The documentation is here.
I also noticed in that documentation that the cocoa instructions tell you to put a copy files phase into the build. I tried that. Didn't change the outcome.
The last time I fell into a sink hole it was because the library was C++ code. Maybe that's still the problem.
Barring a rapid solution here, I am going to go back to using STAsserts, as sickening as that prospect is, this is far, far worse.
Update: reinstalled Xcode. Still doesn't work. There are cheap ways to make this work, like add the header files to the project. Did a blog post about this that brought out a person with the same experience.
I use a number of frameworks in my projects. Some from other people and some are mine. Looking at the documentation I would suggest that the copy phase stuff is not for iOS development. So I would not do that. I downloaded the latest zip from https://github.com/jonreid/OCHamcrest and it appears to contain a ready to go iOS static library. (Not on my mac so I cannot test to confirm).
Anyway, the way I include static libs is to
Select the project (XCode 4).
Select the target I want to add the library to.
Select the Build phases tab.
Expand Link binary with Libraries.
Click the [+] button to add a framework.
Click the [Add Other ...] button and navigate to the directory containing the <lib>.framework directory and select that.
Thats all. The targets search paths will be updated to include the framework directory and the framework will be listed on the left under the project. Expanding it will show the headers.
The problem you mention sound like a couple of things. Firstly the framework not found sounds like the framework has not been included in the target. When you select the framework in the project list on the left, you should be able to see it's Target Membership displayed on the right. Check it's on for the target you are compiling.
Secondly building frameworks is not a trivial task so don't attempt it unless you have the scripts to do it. I say this because building a iOS static framework means compiling for both simulator and devices, combining the compiled lib files into a universal one, and then storing it and the header in a specific directory strucuture.
The downloaded zip from OCHamcrest though, appears to have the correct OCHamcrestiOS.framework in it. So if you store that directory somewhere and link to it using the steps I've outlined above it should work just fine.
So the solution I adopted for now, after much thrashing around, was to include the framework in the project.
Create a group inside the Xcode project called Third Party.
Link it to a folder called thirdparty.
Go to the Add Files in Xcode and pick the framework.
Drag it over to the dependencies.
Run the tests, they pass!
This is maybe preferable anyway because referencing it in /System/Library/Frameworks would present some versioning issues, and this can be setup very quickly. Furthermore, we have a continuous integration server, and having to get in there and sync versions whenever something changes is not so great.
Thanks for checking it out, Derek.

How do I change an existing Xcode target from dynamic to static?

I'm working with an existing project that produces a dynamic library (Cocoa API).
I'd rather generate a static library, but if I change the
[Linking|Mach-O Type] field from "Dynamic Library" to "Static Library",
both the Clean Project and Build Project complain that the
target has an invalid MACH_O_TYPE value of 'staticlib'.
Is there a straightforward way to get the build to produce
a static .a file?
Thanks,
Eric
Opens up the project.pbxproj file in YourProjectName.xcodeproj folder using TextEdit, search for productType and change it's value from "com.apple.product-dynamic" to "com.apple.product-static"
I ended up creating a new 'static library' project, and then added all
the members. Closing xcode and bringing up the two project files in
a text editor let me quickly complete the new project.
I managed to do this, with the help of this post and a bit of digging around. Additional changes I had to make were changing compiled.mach-o.dylib to archive.ar and changing various references in the project file (including inside comments, call me pedantic) from foo.dylib to libfoo.a.
I also had to create a new 'scheme' before it would build, but that might be because I changed the name of the project, I'm not sure. Also, any frameworks referenced by the library need to be added to the application(s) that link against it when you move from .dylib to .a.
It was well worth persevering though as it preserved all the subtleties of the original project (such as building a 32/64 fat binary for release but not for debugging). Result.
Delete your build target, then create a new one, choose "Library" and make sure you choose type "Static". After this, you only have to add your sources and dependencies again.
You have to change two settings:
Under Library, set Mach-O Type to Static Library
Under Packaging, set Executable Extension to a
This worked for me on Xcode 13.1.

Adding a framework in Xcode

Im trying to add a third party framework in Xcode but I'm getting an error that it can't be found
error: MKAbeFook/MKAbeFook.h: No Such File or Directory
I've added it in the Linked Frameworks group and added it to the Target for this project. I've also added it to a new Copy Files build phase for that target.
Basically I've followed the instructions here: http://pointlesscrap.net/mkabefook/desktop/gettingstarted
Any ideas where I'm going wrong?
Best way to diagnose this is to post the compiler invocation line, not just the error (you can just drag and drop the line from the Build Results window that says "Compiling foo.m"). My expectation is that you don't have a Frameworks Search Path set up for the location of the framework. Xcode usually adds that manually when you add the framework, but if you put it in certain kinds of places, or added it in certain ways, you need to manually point your target's Framework Search Paths at its directory.
Perhaps the author mistook MKFaceBook for MKAbeFook?
EDIT: Never mind, it looks like that is actully the name of the framework.

Resources