课堂笔记2

一、Makefile

make命令执行时,需要一个Makefile文件,以告诉make命令需要怎样去编译和链接程序。

格式:target(目标):dependency(依赖)

                   (Tab字符)command(命令)

例:

hello:hello.c

      gcc hello.c -o hello

通过hello.o和print.o文件生成hello,使用变量方便编程。

Target = hello

Object = hello.o print.o

$(Target):$(Object)

    gcc $(Object) -o $(Target)

hello.o:hello.c

    gcc -c hello.c -o hello.o

print.o:print.c

    gcc -c print.c -o print.o

make只能编译最新文件:

make clean

.PHONY:clean     //声明clean为伪目标

clean:

    rm *.o hello

用gcc编译多个文件:gcc hello.c print.c -o hello

二、gdb调试工具

编译时使用gcc xx.c -o xx -g

gdb xx进入调试

(1)l+行号:显示从该行开始的十行

(2)加断点:b 行号或者 break 函数名

查看断点信息:info b  删除断点:delete b

(3)执行:run/r

(4)print x打印变量值

(5)continue继续向下执行

        return退出函数

       quit/q退出

(6)n/s:进入下一条语句。区别:n不进入函数,s进入函数

三、shell脚本

文件后缀为.sh

加固定格式头文件:#!/bin/bash 或者 #!/bin/sh

例:脚本输出helloworld、hello、参数

#!/bin/bash

Tmp="hello"

echo "helloworld"

echo ${Tmp}

echo "first $1"

echo "second $2"

echo "all $*"

echo "sum $#"

echo为输出命令,echo “hello”> xx     (>表示覆盖,>>表示追加)

脚本在执行前不需要编译为二进制文件,执行前需要用chmod修改权限,执行./xx.sh +参数1 参数2……

 $*:输出所有参数,$#:输出参数总数

脚本中注释用#

例:在脚本中利用循环创建目录、文件

#!/bin/bash

for dir in $1 $2 $3 $4               #循环固定格式

do

    mkdir $dir                            #创建目录

    cd $dir                                  #进入目录

    touch $dir                             #创建文件

    echo "hello $dir" > $dir        #在文件中写入hello xx

    cd ..                                       #返回上一层

done

判断是文件还是目录:

#!/bin/bash

PATH = $1               #$1路径在执行时写入

if [ -z $1 ]; then       #判断是否输入了命令行参数,exit退出整个脚本

 echo "ERROR"

 exit

fi

if [ -d $1 ]; then

 echo "DIR"

elif [ -f $1 ]; then

 echo "FILE"

fi

脚本中的选择 switch case

判断从键盘输入的是数字、小写字母、大写字母还是特殊字符:

#!/bin/bash

read key

case "${key}" in

[A-Z] ) echo "UpperLetter";;

[a-z] ) echo "LowerLetter";;

[0-9] ) echo "Number";;

* ) echo "ERROR";;

esac

需要修改export LANG=C,改变编码顺序。

在脚本中输出helloworld:

#!/bin/bash

file = "hello.c"

target = "hello"

echo "#include <stdio.h>" > $file

echo "int main()" >> $file

……

echo 'printf("helloworld\n")' >> $file

……

gcc $file -o $target

./$target

如果使用cat > $file << EOF,可以不使用echo

四、基本数据类型

常见数据类型占得字节数:int 4      short 2     long 4     char 1     float 4     double 8

unsigned int类型整数范围大于int类型,相同类型的数据才能进行运算。有符号整型和无符号整型运算,要将有符号类型用补码形式转化成无符号整型再进行运算。乘法要考虑越位问题。%u为无符号整型

const:修饰只读变量,加了const不能通过修改变量的值来改变变量内存的值。 %p打印地址(用十六进制表示)

volatile:防止编译的时候被优化

\n 换行  \t空一个tab键距离

条件运算符:(a>b)? a : b

区分++i,--i和i++,i--

int a=4;

a+=a++;  (a=9)

a+=++a;  (a=10)

++a+=a;  (a=10)

++a+=a++;  (a=11)

++a+=++a;  (a=12)

++a最先执行


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值