iOS編譯Libevent靜態(tài)庫

感謝先行

Libevent github地址
機(jī)型架構(gòu)參考
感謝分享1
感謝分享2

步驟

  • 找一個(gè)文件夾败玉,進(jìn)入到該文件夾
  • touch build_libevent.sh
  • 感謝分享1中的README.md的內(nèi)容復(fù)制到build_libevent.sh中,記得點(diǎn)個(gè)star金拒,都用了別人的勞動(dòng)成果了索守。
  • 在新的bash里xcodebuild -showsdks 如下圖
    截屏2022-01-20 上午11.17.06.png

然后修改 下述代碼塊中的前兩行

###########################################################################
# Choose your libevent version and your currently-installed iOS SDK version:
#
VERSION="2.1.12-stable"     #Choose your libevent version
USERSDKVERSION="14.4"   #your currently-installed iOS SDK version in last shortcut
MINIOSVERSION="9.0"
VERIFYGPG=false

因?yàn)槲业膌ibevent是用在真機(jī)上的,模擬器不能用辱挥,所以我直接刪除了模擬器相關(guān)的東西舔箭,這個(gè)的話熊痴,可以根據(jù)自己的需要進(jìn)行裁剪

  • 執(zhí)行腳本 ./build_libevent.sh 正常如下
checking whether to build static libraries... yes
checking for library containing inet_ntoa... none required
checking for library containing socket... none required
checking for library containing inet_aton... none required
checking for library containing sendfile... none required
checking for WIN32... no
checking for MIDIPIX... no
checking for CYGWIN... no
checking zlib.h usability... yes
checking zlib.h presence... yes
checking for zlib.h... yes
checking for library containing inflateEnd... -lz
checking for special C compiler options needed for large files... no
checking for _FILE_OFFSET_BITS value needed for large files... no
checking for arm-apple-darwin-pkg-config... no
checking for pkg-config... no
checking for library containing SSL_new... no
checking for library containing SSL_new... no
checking openssl/ssl.h usability... no
checking openssl/ssl.h presence... no
checking for openssl/ssl.h... no
configure: error: openssl is a must but can not be found. You should add the directory containing `openssl.pc' to the `PKG_CONFIG_PATH' environment variable, or set `CFLAGS' and `LDFLAGS' directly for openssl, or use `--disable-openssl' to disable support for openssl encryption

使用

記得把dependencies目錄下的include 文件夾加入你的工程乳蓄,并在Build Settings -> Search Paths 加入這個(gè)引用咪橙。否則會(huì)報(bào)#include <event2/util.h> not found

截屏2022-01-21 下午2.18.38.png

如果遇到如下的命令行報(bào)錯(cuò),就說明你的sdk版本不對(duì),仔細(xì)檢查xcodebuild -showsdks和你的腳本寫的

?  build-ios git:(master) ? ./build_libevent.sh
Using libevent-2.1.12-stable.tar.gz
Building without ccache
??
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for arm-apple-darwin-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... build-aux/install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking whether make supports the include directive... yes (GNU style)
checking for arm-apple-darwin-gcc... /usr/bin/gcc -arch armv7 -miphoneos-version-min=9.0
checking whether the C compiler works... no
configure: error: in `/Users/draven/Desktop/speedup/third-party/libevent/build-ios/build/src/libevent-2.1.12-stable':
configure: error: C compiler cannot create executables
See `config.log' for more details

至此美侦,configure執(zhí)行了产舞,但是少了依賴項(xiàng)。

configure: error: openssl is a must but can not be found. You should add the directory containing `openssl.pc' to the `PKG_CONFIG_PATH' environment variable, or set `CFLAGS' and `LDFLAGS' directly for openssl, or use `--disable-openssl' to disable support for openssl encryption

最后貼上大佬的腳本

###########################################################################
# Choose your libevent version and your currently-installed iOS SDK version:
#
VERSION="2.1.8-stable"
USERSDKVERSION="10.3"
MINIOSVERSION="8.0"
VERIFYGPG=false

###########################################################################
#
# Don't change anything under this line!
#
###########################################################################

# No need to change this since xcode build will only compile in the
# necessary bits from the libraries we create
ARCHS="i386 x86_64 armv7 armv7s arm64"

DEVELOPER=`xcode-select -print-path`
#DEVELOPER="/Applications/Xcode.app/Contents/Developer"

# for continuous integration
# https://travis-ci.org/mtigas/iOS-OnionBrowser
if [ "$1" == "--noverify" ]; then
    VERIFYGPG=false
fi
if [ "$2" == "--travis" ]; then
    ARCHS="i386 x86_64"
fi

if [[ ! -z "$TRAVIS" && $TRAVIS ]]; then
    # Travis CI highest available version
    echo "==================== TRAVIS CI ===================="
    SDKVERSION="${USERSDKVERSION}"
else
    SDKVERSION="${USERSDKVERSION}"
fi

cd "`dirname \"$0\"`"
REPOROOT=$(pwd)

# Where we'll end up storing things in the end
OUTPUTDIR="${REPOROOT}/dependencies"
mkdir -p ${OUTPUTDIR}/include
mkdir -p ${OUTPUTDIR}/lib


BUILDDIR="${REPOROOT}/build"

# where we will keep our sources and build from.
SRCDIR="${BUILDDIR}/src"
mkdir -p $SRCDIR
# where we will store intermediary builds
INTERDIR="${BUILDDIR}/built"
mkdir -p $INTERDIR

########################################

cd $SRCDIR

# Exit the script if an error happens
set -e

if [ ! -e "${SRCDIR}/libevent-${VERSION}.tar.gz" ]; then
    echo "Downloading libevent-${VERSION}.tar.gz"
    curl -LO https://github.com/libevent/libevent/releases/download/release-${VERSION}/libevent-${VERSION}.tar.gz
fi
echo "Using libevent-${VERSION}.tar.gz"

# up to you to set up `gpg` and add keys to your keychain
# may have to import from link on http://www.wangafu.net/~nickm/ or http://www.citi.umich.edu/u/provos/
if $VERIFYGPG; then
    if [ ! -e "${SRCDIR}/libevent-${VERSION}.tar.gz.asc" ]; then
        curl -LO https://github.com/libevent/libevent/releases/download/release-${VERSION}/libevent-${VERSION}.tar.gz.asc
    fi
    echo "Using libevent-${VERSION}.tar.gz.asc"
    if out=$(gpg --status-fd 1 --verify "libevent-${VERSION}.tar.gz.asc" "libevent-${VERSION}.tar.gz" 2>/dev/null) &&
    echo "$out" | grep -qs "^\[GNUPG:\] VALIDSIG"; then
        echo "$out" | egrep "GOODSIG|VALIDSIG"
        echo "Verified GPG signature for source..."
    else
        echo "$out" >&2
        echo "COULD NOT VERIFY PACKAGE SIGNATURE..."
        exit 1
    fi
fi

tar zxf libevent-${VERSION}.tar.gz -C $SRCDIR
cd "${SRCDIR}/libevent-${VERSION}"

set +e # don't bail out of bash script if ccache doesn't exist
CCACHE=`which ccache`
if [ $? == "0" ]; then
    echo "Building with ccache: $CCACHE"
    CCACHE="${CCACHE} "
else
    echo "Building without ccache"
    CCACHE=""
fi
set -e # back to regular "bail out on error" mode

export ORIGINALPATH=$PATH

for ARCH in ${ARCHS}
do
    if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ];
    then
        PLATFORM="iPhoneSimulator"
        EXTRA_CONFIG="--host=${ARCH}-apple-darwin"
    else
        PLATFORM="iPhoneOS"
        EXTRA_CONFIG="--host=arm-apple-darwin"
    fi

    mkdir -p "${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"

    export PATH="${DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin/:${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/usr/bin/:${DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin:${DEVELOPER}/usr/bin:${ORIGINALPATH}"
    export CC="${CCACHE}`which gcc` -arch ${ARCH} -miphoneos-version-min=${MINIOSVERSION}"

    ./configure --disable-shared --enable-static --disable-debug-mode ${EXTRA_CONFIG} --disable-clock-gettime \
    --prefix="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" \
    LDFLAGS="$LDFLAGS -L${OUTPUTDIR}/lib" \
    CFLAGS="$CFLAGS -Os -I${OUTPUTDIR}/include -isysroot ${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk" \
    CPPFLAGS="$CPPFLAGS -I${OUTPUTDIR}/include -isysroot ${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk"

    # Build the application and install it to the fake SDK intermediary dir
    # we have set up. Make sure to clean up afterward because we will re-use
    # this source tree to cross-compile other targets.
    make -j$(sysctl hw.ncpu | awk '{print $2}')
    make install
    make clean
done

########################################

echo "Build library..."

# These are the libs that comprise libevent. `libevent_openssl` and `libevent_pthreads`
# may not be compiled if those dependencies aren't available.
OUTPUT_LIBS="libevent.a libevent_core.a libevent_extra.a libevent_openssl.a libevent_pthreads.a"
for OUTPUT_LIB in ${OUTPUT_LIBS}; do
    INPUT_LIBS=""
    for ARCH in ${ARCHS}; do
        if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ];
        then
            PLATFORM="iPhoneSimulator"
        else
            PLATFORM="iPhoneOS"
        fi
        INPUT_ARCH_LIB="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/${OUTPUT_LIB}"
        if [ -e $INPUT_ARCH_LIB ]; then
            INPUT_LIBS="${INPUT_LIBS} ${INPUT_ARCH_LIB}"
        fi
    done
    # Combine the three architectures into a universal library.
    if [ -n "$INPUT_LIBS"  ]; then
        lipo -create $INPUT_LIBS \
        -output "${OUTPUTDIR}/lib/${OUTPUT_LIB}"
    else
        echo "$OUTPUT_LIB does not exist, skipping (are the dependencies installed?)"
    fi
done

for ARCH in ${ARCHS}; do
    if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ];
    then
        PLATFORM="iPhoneSimulator"
    else
        PLATFORM="iPhoneOS"
    fi
    cp -R ${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/include/* ${OUTPUTDIR}/include/
    if [ $? == "0" ]; then
        # We only need to copy the headers over once. (So break out of forloop
        # once we get first success.)
        break
    fi
done


####################

echo "Building done."
echo "Cleaning up..."
rm -fr ${INTERDIR}
rm -fr "${SRCDIR}/libevent-${VERSION}"
echo "Done."


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末菠剩,一起剝皮案震驚了整個(gè)濱河市易猫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌具壮,老刑警劉巖准颓,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異棺妓,居然都是意外死亡攘已,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門涧郊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贯被,“玉大人眼五,你說我怎么就攤上這事妆艘。” “怎么了看幼?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵批旺,是天一觀的道長。 經(jīng)常有香客問我诵姜,道長汽煮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任棚唆,我火速辦了婚禮暇赤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘宵凌。我一直安慰自己鞋囊,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布瞎惫。 她就那樣靜靜地躺著溜腐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瓜喇。 梳的紋絲不亂的頭發(fā)上挺益,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音乘寒,去河邊找鬼望众。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的烂翰。 我是一名探鬼主播叉袍,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼刽酱!你這毒婦竟也來了喳逛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤棵里,失蹤者是張志新(化名)和其女友劉穎润文,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體殿怜,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡典蝌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了头谜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片骏掀。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖柱告,靈堂內(nèi)的尸體忽然破棺而出截驮,到底是詐尸還是另有隱情,我是刑警寧澤际度,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布葵袭,位于F島的核電站,受9級(jí)特大地震影響乖菱,放射性物質(zhì)發(fā)生泄漏坡锡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一窒所、第九天 我趴在偏房一處隱蔽的房頂上張望鹉勒。 院中可真熱鬧,春花似錦吵取、人聲如沸禽额。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绵疲。三九已至,卻和暖如春臣疑,著一層夾襖步出監(jiān)牢的瞬間盔憨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國打工讯沈, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留郁岩,地道東北人婿奔。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像问慎,于是被迫代替她去往敵國和親萍摊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容