PDA

View Full Version : Static & Dynamic Libraries




bluetooth42
Sep 2, 2008, 08:12 PM
Is it possible to link static and/or dynamic libraries built with either gcc/g++ and use them in Xcode? If so, how do you accomplish it? I get the following errors when trying to build. I have added the directory with the header file to the search path. It picks it up in intellisense but it is not able to find it.

main.m:10:16: error: Add.h: No such file or directory
main.m: In function 'main':
main.m:13: error: 'Add' undeclared (first use in this function)
main.m:13: error: (Each undeclared identifier is reported only once
main.m:13: error: for each function it appears in.)
main.m:13: error: 'add' undeclared (first use in this function)

Also, I would like to separately develop a piece of the application outside of Objective C and use the C++ library in the iPhone application. Is this in violation of the SDK agreement and why?

Update: I have successfully compiled the library as a static library.

Here is how to enable static library compilation in Xcode with an iPhone application in Objective C.

1. Compile the objects and create a static library (.a extension)
ar cvq lib<name of library>.a Add.o
2. Add -l<name of library> (e.g. -ladd) to Other Linker Flags
3. Add include path to User Header Search Paths
/Users/user/include
4. Create directory <library name> in /Users/user/include
5. Compile Sources As "Objective-C++"
6. Add Add.h (or other header files) to the user include directory
7. Include the directory in your .mm file
#import "add/Add.h"



PhoneyDeveloper
Sep 2, 2008, 11:40 PM
Just some advice. Xcode is not a makefile. Here's the easy way:

build your .a file
drag your .a file from the Finder into your project window
You're done

(OK you have to now build your project)

Leave the -l and the header search paths for others to fool with.

Also, to avoid the header search path jazz just add the header file to your project (and you're done).

Oh yeah, ar hah hah. You did use an Xcode project to build your .a file didn't you?

If you want Xcode to automatically build the .a file when the project that depends on it requires it you can do that also. Look at the docs.

Taum
Sep 3, 2008, 04:01 AM
Hi,

I don't know much about librairies (as in the difference between static/dynamic, different formats etc.), but I guess you'd have to compile the library to ARM code right ? How do you achieve that and get a .a file that would work on the iphone ?

Also, how do you handle the difference between simulator and iphone in this case ? can they both use the same library (I wouldn't think so, but as I said my knowledge is very limited).

If someone manages to compile code as a library and then link it in an iPhone project, I think a lot of us would be very grateful if you could give some detailed steps for this process. :)