Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Creating a Static Library in iOS Tutorial

Learn how to create a static library in iOS in this tutorial, and how to use it from within different projects and architectures.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2658-creating-a-static-library-in-ios-tutorial

Thanks for posting a great tutorial that still provides value in 2016. I made some tweaks to the Run Script that I’d like to share.

First of all, instead of building to the Products directory which can be hard to find, I am building to the ${SRCROOT} source directory inside a folder called ${PROJECT_NAME}-lib. One advantage of this approach is that the project name doesn’t need to be added to the Run Script. I also delete the local build folder since it is not needed after the universal library is built. One other important change is that when I build the iphonesimulator build I also include ONLY_ACTIVE_ARCH=NO to avoid the error where slices are missing from the final static library.

Additionally, XCode 7.3 has an issue with generating bitcode using the static library Run Script (see the top LOC). I am also using a User-Enabled Build Setting called BITCODE_GENERATION_MODE with a value of bitcode, but this is configured within my Target Settings in XCode.

# workaround for bitcode generation problem with Xcode 7.3
unset TOOLCHAINS

# configure directories for intermediate and final builds
UNIVERSAL_OUTPUTFOLDER=${SRCROOT}/${PROJECT_NAME}-lib

# build device and simulator versions
xcodebuild -target ${PROJECT_NAME} ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
xcodebuild -target ${PROJECT_NAME} ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# create universal binary file using lipo
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a"

# copy the header files to the final output folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/include" "${UNIVERSAL_OUTPUTFOLDER}/"

# remove the build folder
rm -rf ${SRCROOT}/build

Hi @ernesto I downloaded your project starter project for this section for Method 1: Header and Library Binary Files and I am getting the following error “No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).” please help me. Another question is can I directly drag “libImageFilters.a” and “ImageFilters.h” to my new project I am asking this because you have included those files directly in the Zip Folder along with the project.

Thanks for the great tutorial for adding subproject as library for debugging purposes. Compiling perfect

I am facing an issue that the images which are a part of sub-project are not appearing in main project.
I get black screen when ViewControllers from sub-project are pushed on the main-project.

Cheers!
Jeet

@kanna18 I was getting this same error. I got rid of it by going to “Build Setting” and changing “Build Active Architecture Only” from “Yes” to “No”. I found this solution on Stack Overflow: http://stackoverflow.com/questions/21985909/no-architectures-to-compile-for-only-active-arch-yes-active-arch-x86-64-valid

Hey Thanks so much, Its Nice Tutorial, clear and neat
I was wondering if you could also give some insight on converting an existing project into a static library which uses different cocoapods libraries, storyboard, DBManager files and all the files that a basically normal app can have and is it even possible to do so.

Thanks

I am following the below tutorial. I am facing the" clang: error: linker command failed with exit code 1 (use -v to see invocation)" .I have tried both the method. I am using Xcode 8.

This tutorial is more than six months old, so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]