脚本的入门学习 Shell

702 阅读1分钟

练习创建Framework 以及链接Framework: juejin.cn/post/692651…
静态库 .a的原理与创建 : juejin.cn/post/692616…

继上述两篇文章需要在终端输入很多的命令,输入错的时候很难修改繁琐,所以利用脚本来编译方便使用

回到我们一开始的项目主要的任务是将StaticLibrary里面的TestExample.m编译成.o,test.m编译成.o然后.o编译成可执行文件

  • 编译TestExample.o
clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-c TestExample.m -o TestExample.o \
ar -rc libTestExample.a TestExample.o
  • 编译test.o
clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-I./StaticLibrary \
-c test.m -o test.o
  • test.o编译成可执行文件
 clang -target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-L./StaticLibrary \
-lTestExample \
test.o -o test

开始写脚本

  • 1.创建一个build.sh 脚本文件
  • 2.源码
pushd ./StaticLibrary
clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-c TestExample.m -o TestExample.o
ar -rc libTestExample.a TestExample.o
popd
clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-I./StaticLibrary \
-c test.m -o test.o

 clang -target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-L./StaticLibrary \
-lTestExample \
test.o -o test
  • push :在当前的目录栈中进入对应的目录
  • popd :返回上一个目录
  • 不使用cd 直接修改pwd目录栈的最上层元素,修改完之后是无法返回的
  • cd 到Shell 目录
    • ./build.sh
    • 报错 -没有可执行的权限
  • 加权限 chmod +x ./build.sh
  • 再次执行 ./build.sh
  • 看到已经成功了

优化脚本 添加注释

  • echo 打印
echo "---开始编译 TestExample.a"
echo "---进入StaticLibrary目录"
pushd ./StaticLibrary
clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-c TestExample.m -o TestExample.o
ar -rc libTestExample.a TestExample.o

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

echo "---编译test.o"
clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-I./StaticLibrary \
-c test.m -o test.o
echo "---编译test.exec"
 clang -target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk \
-L./StaticLibrary \
-lTestExample \
test.o -o test

  • 变量的定义
    • ${变量} 或者 $变量
SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk
FILE_NAME=test
HEADER_SEARCH_PATH=./StaticLibrary
TARGET=x86_64-apple-macos11.1
echo "---开始编译 TestExample.a"
echo "---进入StaticLibrary目录"
pushd ${HEADER_SEARCH_PATH}
clang -x objective-c \
-target ${TARGET} \
-fobjc-arc \
-isysroot $SYSROOT \
-c TestExample.m -o TestExample.o
ar -rc libTestExample.a TestExample.o

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

echo "---编译test.o"
clang -x objective-c \
-target ${TARGET} \
-fobjc-arc \
-isysroot $SYSROOT \
-I${HEADER_SEARCH_PATH} \
-c ${FILE_NAME}.m -o ${FILE_NAME}.o
echo "---编译test.exec"
 clang -target ${TARGET} \
-fobjc-arc \
-isysroot $SYSROOT \
-L${HEADER_SEARCH_PATH} \
-lTestExample \
${FILE_NAME}.o -o ${FILE_NAME}