Eric
Eric

Reputation: 2275

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

Upvotes: 15

Views: 15317

Answers (5)

craig65535
craig65535

Reputation: 3582

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.

Upvotes: 0

mous
mous

Reputation: 331

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.

Upvotes: 0

catnip
catnip

Reputation: 25388

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.

Upvotes: 2

Ricky Lung
Ricky Lung

Reputation: 680

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"

Upvotes: 23

Eric
Eric

Reputation: 2275

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.

Upvotes: 4

Related Questions