开发时用man看一些函数的说明,经常看到一些整型,一时不知道它的字节大小及符号,只能编译运行之后才能得知。因此写了下面这个小脚本,用于在编译时确定整型的大小及符号(包括交叉编译)。
#!/bin/sh
# 本脚本主要用于在编译时确定各种整型的大小及符号(包括交叉编译)。
# 这些信息,编译时就可以确定下来。主要利用了数组大小不能为负,sizeof及编译时计算等特性。
# static int a[sizeof(char)-33];
# static int a[(((signed int)~0) < 0)-1];
#
# 由于判断某些数据类型时,需要引入特定的头文件,因此调用此脚本的基本格式为:
# ./check_type.sh gcc xxx.h yyy.h data_type
# 其中gcc,xxx.h,yyy.h这三个参数是可以省略的,默认就用gcc,不包含头文件。
#
# 在脚本的实现里,是根据入参的后缀来判断用户输入的是什么,如果后缀是.h,则认为是头文件,
# 如果后缀是gcc,则认为是指定编译器,否则认为是指定数据类型(如果是两个或多个字符串指定的数据类型,比如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函数
#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
}
# 先判断是否是整型(如果是结构体等,将会报错)
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