views:

1044

answers:

2

Hi,

I have never been clearly understand all these linking/building/dependency business. Now, I am trying to build the FreeType library (which is in C++), into the *.a library file for the iPhone (because another library I am trying to use, openFrameworks, would depend on FreeType).

I know that to compile C++ with iPhone I simply need to rename *.cpp to *.mm. (I have tried with some simply programs in main() and it works) But how can I build the library in Xcode then ? (without the main() I suppose)

On the other hand, it would be great if you guys could recommend some books or documents on such linking/building topics, which I found myself most confused about.

PS. I have already got the paid version of Xcode and some sample apps compiled onto the iPhone.

A: 

First, you do not need to rename any files to compile C++ code for the iPhone. Secondly, you'll want to create a separate project for the library. It looks like the iPhone target types don't include "static library", so you might need to create a Mac OS X project and modify it to output an ARM .a file. Not trivial, but probably doable.

Alternatively, if you're not going to be using this code in a lot of otherwise unrelated projects, you could include the source files directly into your App project. Collect them all into their own group, and you'll hardly know they are there.

Mark Bessey
+3  A: 

Renaming .cpp files to .mm would mean they'll be treated as Objective-C++ code rather than C++ code. I don't think that'd be a good idea, even if it should still work. Besides, FreeType is written in C, not C++.

Google for "compiler linker" and you'll find quite a few documents on how they work. That should help for documentation.

It's entirely possible to compile static libraries for the iPhone; what you can't do is compile dynamic libraries or frameworks - you could, but it's not encouraged.

  1. Open your project
  2. In the source/target browser, select the "Targets" node (with the little red and white target icon).
  3. Right-click on the node, and in the popup menu select "Add", followed by "New Target".
  4. A dialog opens. On the left hand side, there's an iPhone OS and a Mac OS X section. The iPhone OS section should already be selected; if not do so.
  5. You should have three choices of targets, one of it is a static library. Select it, click OK.
  6. Give the library a name in the next page. Click finish.
  7. Your "Targets" node has includes a child for your static library now. You can add sources either via the file menu, or by dragging it onto the "Compile Sources" child node.

Hope that helps.

it's so detail and helpful. thank you very much!
ivanTheTerrible