views:

858

answers:

3

I'm trying to link the libpng library to my iphone application, and getting this error:

ld: warning: in /opt/local/lib/libpng.dylib, file is not of required architecture

This happens when I build for the simulator or the device.

When I build a console application however, I can link in libpng just fine.

What am I doing wrong? Is there a different libpng.dylib I need to use for iphone development?

+1  A: 

Is this a libpng.dylib you downloaded from the Internet? iPhone developers aren't allowed to use outside dynamic libraries.

I don't know if libpng is available, but here's how to check.

  1. Double-click on your app name in the Targets list.
  2. Click the Plus button under the Frameworks list and try to find it. Don't forget that lowercase names appear after "Z".

If it's not there, you can't use it. Try finding a static library and compile it from source. It has to be compiled specifically for both the architecture of the iPhone Simulator (Intel) and iPhone (ARM).

Chris Long
It's the libpng.dylib that was installed from macports. I tried linking statically too with no success. Libpng isn't in that list of available frameworks.
Eric Maxey
+1  A: 

As Chris Long says, libpng is not available on the iphone. You can compile it into your project (if it will compile for ARM), otherwise it no workums. Here is a lead: http://www.cloudgoessocial.net/2009/06/09/imagemagick-on-iphone-with-jpeg-png/

Also, for the record, you can write out a UIImage as png natively using: UIImagePNGRepresentation(UIImage *image)

Kenny Winker
That link has a libpng.a that contains both i386 and arm architectures, which exactly what I need. Unfortunately, when I use that file, it still gives me the same error as above, even referencing "/opt/local/lib/libpng.dylib" which I have removed from my project.
Eric Maxey
+2  A: 

You can't use dylibs on iPhone, you will need to compile it statically. Having said that, the error you are seeing is separate issue.

Are you developing on Snow Leopard? If so then then the default library you built is likely 64 bit. The simulator requires 32 bit libraries. You can check by running file against it:

file /opt/local/lib/libpng.dylib

If you don't see something like:

(for architecture i386):    Mach-O dynamically linked shared library i386

Then you won't be able to reuse it. Rebuild the library as a static i386 lib and you should be able to use it in the simulator. If it is an autoconf based build this blog post explains how to do a build and has shell script that you can use.

Louis Gerbarg
Thank you! I am on Snow Leopard, and my libpng is indeed 64 bit. That didn't even cross my mind. This is all making much more sense now.
Eric Maxey