Including Third party libraries(Example : AFNetworking) in static library

徘徊边缘 提交于 2020-01-02 08:47:40

问题


Is it possible to include 3rd party libraries in static library? Example : Can AFNetworking can be included in static library


回答1:


The direct answer to your question is YES, you can definitely include any third party library if they expose a public API (a set of headers for you to refer). For AFNetworking, they have made it so simple by providing a Cocoapods script so that your project can refer it.

But be aware that when you release your static library with including AFNetwork inside, and someday if your static lib user decides to use AFNetwork in his own code, the Obj-C compiler will complain about duplicate symbols and he will be unable to build his project with your static lib.

My advice

My advice :just refer to the link shared by @Amar above. It is very important NOT to include any third party libraries in your static library if you are hoping to share it with other developers or community. Always consider using references for other third party static libraries instead of including them, for example make use of Cocoapods.




回答2:


Just for completeness, because I think that the previous answers/comments provide good advice, this is a custom script to include a third party lib to the output lib of a build

set -e
set +u

TGT_FULLPATH="${BUILT_PRODUCTS_DIR}/${EXECUTABLE_NAME}"
TMP_FULLPATH="${BUILT_PRODUCTS_DIR}/original_${EXECUTABLE_NAME}"
THIRPARTY_FULLPATH=...
ARCHSPECIFIC_THIRDPARTY="${BUILT_PRODUCTS_DIR}/thinThirdparty"

# What's the architecture for the lib we just built?
LIPO_ARCH=$(lipo -info  ${TGT_FULLPATH} | awk 'END{ print $NF }')

# Create a thirdparty lib only for the current architecture
lipo -thin ${LIPO_ARCH} ${THIRPARTY_FULLPATH} -output ${ARCHSPECIFIC_THIRDPARTY}

# Join the two libaries
mv ${TGT_FULLPATH} ${TMP_FULLPATH}
libtool -static -o ${TGT_FULLPATH} ${TMP_FULLPATH} ${ARCHSPECIFIC_THIRDPARTY} 2>&1  >/dev/null 

# Remove the temp artifacts
rm ${TMP_FULLPATH}
rm ${ARCHSPECIFIC_THIRDPARTY}


来源:https://stackoverflow.com/questions/19442593/including-third-party-librariesexample-afnetworking-in-static-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!