15

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

iCaramba
  • 2,589
  • 16
  • 31
Eric
  • 2,115
  • 2
  • 20
  • 29

5 Answers5

23

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"

Ricky Lung
  • 680
  • 7
  • 12
  • 10
    in my case it's **com.apple.product-type.library.dynamic** to be changed into **com.apple.product-type.library.static**. Maybe it's a different xcode version, I suggest to create a new static lib project and copy productType from there – pqnet Aug 25 '11 at 09:18
  • 3
    For Xcode 9: static library is `com.apple.product-type.library.static`, dynamic library is `com.apple.product-type.library.dynamic`, framework is `com.apple.product-type.framework`, generic bundle is `com.apple.product-type.bundle`, unit testing bundle is `com.apple.product-type.bundle.unit-test`, application is `com.apple.product-type.application`, and command-line tool is `com.apple.product-type.tool`. There are many more, but these are by far the most common – ThatsJustCheesy Feb 18 '18 at 23:18
4

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.

Eric
  • 2,115
  • 2
  • 20
  • 29
2

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.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
0

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.

mous
  • 331
  • 1
  • 9
0

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.

craig65535
  • 3,439
  • 1
  • 23
  • 49