编译原理2-链接framework

使用libtool创建framework中的库文件
虽然文件结构貌似一样,但是不能直接使用libTestExample.a,需要通过libtool创建的静态链接库才行

cd StaticLibrary
libtool -static -o TestExample libTestExample.a

之后才使用clang链接成可执行文件

clang -target x86_64-apple-macos12.0 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk \
-F./Frameworks \
-framework TestExample \
test.o -o test

以下为全部链接Framework生成可执行文件脚本

LANGUAGE=objective-c
TAREGT=x86_64-apple-macos12.0
SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk

FILE_NAME=test
STATICLIBRARY=TestExample
HEAD_PATH=./StaticLibrary
LIBRARY_PATH=./StaticLibrary

echo "-------------编译test.m to test.o------------------"
clang -x $LANGUAGE  \
-target $TAREGT     \
-fobjc-arc          \
-isysroot $SYSROOT  \
-I${HEAD_PATH}   \
-c ${FILE_NAME}.m -o ${FILE_NAME}.o

echo "-------------进入到StaticLibrary目录------------------"
pushd ${HEAD_PATH}
echo "-------------编译TestExample.m to TestExample.o------------------"
clang -x $LANGUAGE  \
-target $TAREGT     \
-fobjc-arc          \
-isysroot $SYSROOT  \
-c ${STATICLIBRARY}.m -o ${STATICLIBRARY}.o
echo "-------------ar 编译TestExample.o to libTestExample.a------------------"
ar -rc lib${STATICLIBRARY}.a ${STATICLIBRARY}.o
echo "-------------libtool 编译libTestExample.a to 库文件------------------"
libtool -static -o TestExample libTestExample.a

echo "-------------退出StaticLibrary目录------------------"
popd

echo "-------------创建Framework------------------"

rm -rf Frameworks
mkdir Frameworks
mkdir Frameworks/${STATICLIBRARY}.framework
mkdir Frameworks/${STATICLIBRARY}.framework/headers
cp ${HEAD_PATH}/${STATICLIBRARY}.h Frameworks/${STATICLIBRARY}.framework/headers
cp ${HEAD_PATH}/${STATICLIBRARY} Frameworks/${STATICLIBRARY}.framework



echo "-------------链接Frameworks to test EXEC------------------"
clang -target $TAREGT \
-fobjc-arc \
-isysroot /$SYSROOT \
-F./Frameworks \
-framework TestExample \
test.o -o test

echo "-------------查看 EXEC 文件------------------"
objdump --macho --private-header $FILE_NAME

echo "-------------运行 test EXEC------------------"
# lldb
# file test
# r

你可能感兴趣的:(编译原理2-链接framework)