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
相關文章
相關標籤/搜索