zoukankan      html  css  js  c++  java
  • make

    除非最简单的项目,任何一个商业项目都会包含多个源代码,而且在编译的时候会有很长很复杂的指令。
    编程过程中还需要使用那些很少而且很难记忆的编译选项。
    make是一种控制编译或者重复编译软件的工具。
    make可以自动管理软件的编译内容、方式和时机,从而使程序员把更多的精力集中在编写代码上。
     
     
    make是怎么完成工作的呢?
    makefile是一个文本形式的脚本文件,其中包含一些规则告诉make编译哪些文件,怎么样编译以及在什么条件下编译。
    makefile规则遵循以下通用形式

    target:dependency [dependency[…]]

      command

      command

      […]

    每个command第一个字符必须是tab键,而不是空格键,不然make会报错并停止。
     
     
     
    用vi编辑一个简单的makefile,内容如下:

    start:

      gcc -o hello hello.c

    输入make,makefile的内容执行了。
     
     
     
    稍微复杂的makefile,内容如下:

    start:hello.o

            gcc -o hello hello.o

    hello.o:

            gcc -o hello.o -c hello.c

    target start后面的hello.o代表其下的command依赖与hello.o这个target。所以make先执行了hello.o这个target下的command。
     
     
     
    进一步完善的makefile,内容如下:

    start:hello.o

            gcc -o hello hello.o

    hello.o:

            gcc -o hello.o -c hello.c

    clean:

            rm -f hello.o

    增加了target clean。
    输入make clean,make会直接执行clean其下的command。
     
     
     
    在makefile执行shell命令:

    start:hello.o

            gcc -o hello hello.o

            @echo '---------------ok---------------'

    hello.o:

            gcc -o hello.o -c hello.c

    clean:

            rm -f hello.o增加了target clean

    增加了@echo,显示编译成功语句,为了不将语句本身输出,所以前面加@符号。
     
     
     
    为了简化编辑和维护makefile,可以在makefile中使用变量。

    arname=some_text

    把变量用括号括起来,前面加$就可以引用该变量的值。

    $(varname)

    按照惯例makefile的变量都是大写(只是习惯而已,不是必须的)。
     
     
    在makefile使用变量:

    CC=gcc

    start:hello.o

            $(CC) -o hello hello.o

            @echo '---------------ok---------------'

    hello.o:

            $(CC) -o hello.o -c hello.c

    clean:

            rm -f hello.o

    增加变量CC,每个引用变量CC的地方的展开成变量的值。
     
     
     
    在makefile使用变量:

    CC=gcc

    SRCS=hello.c

    OBJS=hello.o

    EXEC=hello

    start:hello.o

            $(CC) -o $(EXEC) $(OBJS)

            @echo '---------------ok---------------'

    hello.o:

            $(CC) -o $(OBJS) -c $(SRCS)

    clean:

            rm -f hello.o

    增加变量SRCS、OBJS、EXEC,每个引用变量CC的地方的展开成变量的值。
     
     
     
    在makefile使用变量:

    CC=gcc

    SRCS=hello.c

    OBJS=$(SRCS:.c=.o)

    EXEC=hello

    start:hello.o

            $(CC) -o $(EXEC) $(OBJS)

            @echo '---------------ok---------------'

    hello.o:

            $(CC) -o $(OBJS) -c $(SRCS)

    clean:

            rm -f hello.o

    OBJS=$(SRCS:.c=.o),意思是将SRCS变量中的.c替换为.o。
     
     
     
    模式规则

    .SUFFIXES:.c .o

    表示任何x.c文件与x.o关联
      –.c.o:
    表示make定义了一条规则,任何x.o文件都从 x.c编译而来
    umake定义了一些有用的预定义变量
     
     

    变量名

    含  义

    $@

    规则的目标所对应的文件名

    $<

    规则中的第一个相关文件名

     
     
    在makefile使用自动变量和模式规则的例子

    .SUFFIXES:.c .o

    CC=gcc

    SRCS=hello.c

    OBJS=$(SRCS:.c=.o)

    EXEC=hello

    start:$(OBJS)

            $(CC) -o $(EXEC) $(OBJS)

            @echo '---------------ok---------------'

    .c.o:

            $(CC) -o $@ -c $<

    clean:

            rm -f $(OBJS)

    常见的make出错信息:
      –No rule to make target ‘target’.Stop
      –makefile中没有包含创建指定target所需要的规则,而且也没有默认规则可用。
      –‘target’ is up to date
      –指定的target相关文件没有变化。
      –command:Command not found
      –make找不到命令,通常是因为命令被拼写错误或者不在$PATH路径下。
     
     
     
  • 相关阅读:
    tornado简单的验证码
    python分页和session和计算时间差
    初始tornado框架
    Jquery小实例
    DOM+Javascript一些实例
    Javascript
    CSS拾遗+技巧集合
    css样式基础
    KVM NAT网络模式配置
    Ultimate guide to learning AngularJS in one day
  • 原文地址:https://www.cnblogs.com/shichuan/p/4475177.html
Copyright © 2011-2022 走看看