開發(fā)時用man看一些函數(shù)的說明,經(jīng)??吹揭恍┱?,一時不知道它的字節(jié)大小及符號,只能編譯運(yùn)行之后才能得知。因此寫了下面這個小腳本,用于在編譯時確定整型的大小及符號(包括交叉編譯)。
#!/bin/sh
# 本腳本主要用于在編譯時確定各種整型的大小及符號(包括交叉編譯)。
# 這些信息,編譯時就可以確定下來。主要利用了數(shù)組大小不能為負(fù),sizeof及編譯時計(jì)算等特性。
# static int a[sizeof(char)-33];
# static int a[(((signed int)~0) < 0)-1];
#
# 由于判斷某些數(shù)據(jù)類型時,需要引入特定的頭文件,因此調(diào)用此腳本的基本格式為:
# ./check_type.sh gcc xxx.h yyy.h data_type
# 其中g(shù)cc,xxx.h,yyy.h這三個參數(shù)是可以省略的,默認(rèn)就用gcc,不包含頭文件。
#
# 在腳本的實(shí)現(xiàn)里,是根據(jù)入?yún)⒌暮缶Y來判斷用戶輸入的是什么,如果后綴是.h,則認(rèn)為是頭文件,
# 如果后綴是gcc,則認(rèn)為是指定編譯器,否則認(rèn)為是指定數(shù)據(jù)類型(如果是兩個或多個字符串指定的數(shù)據(jù)類型,比如long long,需要用雙引號包含住)。
# 參考用法如下所示:
# ./check_type.sh aarch64-himix100v630r3-linux-gcc unistd.h pid_t
# ./check_type.sh int
# ./check_type.sh "long long"
header=""
gcc_cmd="gcc"
target=""
while [ $# -ge 1 ] ; do
# 取字符串末尾兩個字符
suffix=${1:(-2)}
if [ "$suffix" == ".h" ] ; then
header+=$1" "
else
# 取字符串末尾三個字符
suffix=${1:(-3)}
if [ "$suffix" == "gcc" ] ; then
gcc_cmd=$1
else
target=$1
fi
fi
shift
done
if [ -z "$target" ] ; then
echo no target
exit
fi
src_prefix=`mktemp -p ./ tmp.XXXXXXXX`
src_h=$src_prefix".h"
src_c=$src_prefix".c"
mv $src_prefix $src_h
if [ ! -z "$header" ] ; then
for h in $header; do
echo "#include<"$h">" >> $src_h
done
fi
compile() {
# 頭文件
cat $src_h > $src_c
# 測試語句
echo $1 >> $src_c
# main函數(shù)
#echo "int main(int argc, char *argv[]){return 0;}" >> $src_c
# 編譯
$gcc_cmd -S $src_c 2> /dev/null
return $?
}
clean() {
if [ ! -z "$src_prefix" ] ; then
rm $src_prefix.*
fi
}
# 先判斷是否是整型(如果是結(jié)構(gòu)體等,將會報錯)
compile "static int a[(($target)0) == 0];"
if [ $? != 0 ] ; then
echo $target is not a integer
clean
exit
fi
# 判斷符號
compile "static int a[((($target)~0) < 0) - 1];"
if [ $? == 0 ] ; then
echo $target is signed
else
echo $target is unsigned
fi
# 判斷字長大小
for ((i=1;i<=32;i*=2)) ; do
compile "static int a[(sizeof($target) == $i) - 1];"
if [ $? == 0 ] ; then
echo "sizeof($target) = $i"
fi
done
clean