Ubuntu下makefile的简单使用

在Windows下,只须要简单的点击如下make,rebuild便可。而在Linux下,这样的IDE环境并无提供,难道必须每一步都执行一遍吗?比较ok的作法天然是可以利用批处理脚原本进行操做了,这样,只须要修改脚本中须要编译的文件便可。在Linux下,提供了这么一个方便的工具,make。那么接下来咱们来利用make进行程序的组织编译吧。html

1. 编写tool.h工具

#ifndef __TOOL_H
#define __TOOL_H

void printInteger(int number);

#endif // end of tool.h

 2. 编写tool.cui

#include "tool.h"
#include "stdio.h"

void printInteger(int number)
{
        printf("the number is:%d\n",number);
}

 3. 编写main.c命令行

#include "tool.h"
#include "stdio.h"

int main(int argc, char* argv[])
{
        int number;
        number=10;
        printf("Context-Type:text/html\n\n");
        printInteger(number);
        return 0;
}

 4. 编译连接文件生成可执行文件mainhtm

方法1:blog

命令行中进行编译io

4.1.1 编译main.c生成main.o编译

sudo cc -c main.c

 4.1.2 编译tool.c生成tool.oclass

sudo cc -c tool.c

 4.1.3 连接main.o和tool.o生成main可执行文件file

sudo cc -o main main.o tool.o

 4.1.4 运行main

sudo ./main

 方法2:

makefile进行编译连接

4.2.1 编写makefile文件(没有后缀名)

#MakeFile
main:  main.o tool.o

main.o: main.c tool.h
        cc -c main.c

tool.o: tool.c tool.h
        cc -c tool.c

.PHONY:clean
clean:
        rm *.o main

 4.2.2 运行make进行编译连接

sudo make

 4.2.3 运行main

sudo ./main

 4.2.4 删除全部.o文件并再次编译

sudo make clean
sudo make
相关文章
相关标签/搜索