Makefile學習(二)[第二版],makefile學習第二版

來源:互聯網
上載者:User

Makefile學習(二)[第二版],makefile學習第二版
複雜執行個體

#樣本1:在上一個樣本的基礎上再增加一個可執行檔03test[修改之處已標紅]

.PHONY: clean all 

 

CC = gcc 

CFLAGS = -Wall -g

BIN = 01test 02test 03test

SOURCES = $(BIN:=.c)

OBJECTS = $(BIN:=.o)

 

all: $(BIN)

 

01test: 01test.o

02test: 02test.o

03test: 03test.o

 

.c.o:

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

 

clean:

    -rm -rf $(BIN) $(OBJECTS)

 

#樣本2:在樣本1上為03test添加一個自編的標頭檔pub.h[修改之處已標紅]

.PHONY: clean all 

 

CC = gcc 

CFLAGS = -Wall -g

BIN = 01test 02test 03test

SOURCES = $(BIN:=.c)

OBJECTS = $(BIN:=.o)

 

all: $(BIN)

 

01test: 01test.o

02test: 02test.o

03test: 03test.o pub.o

 

.c.o:

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

 

clean:

    -rm -rf $(BIN) $(OBJECTS)

 

一個比較複雜的綜合樣本

//1 pub.cppint pub(int a,char *ch,int b){    char c = ch[0];    switch(c)    {        case '+':            return a+b;            break;        case '-':            return a-b;            break;        case '*':            return a*b;            break;        case '/':            if (b != 0)            {                   return a/b;            }               else            {                   return -1;            }            break;        default:            return -1;            break;    }    return 0;}

//2 pub.hint pub(int a,char *ch,int b);

//3 main.cpp#include <iostream>#include <stdlib.h>#include "pub.h"using namespace std;int main(int argc,char *argv[]){    if (argc < 4)    {           cerr << "error" << endl;        return -1;     }       cout << argv[1] << ' ' << argv[2] << ' ' << argv[3] << " = "          << pub(atoi(argv[1]),argv[2],atoi(argv[3])) << endl;    return 0;}

#4 Mackfile

.SUFFIXES:.cpp .o

 

CC=g++

SOURCE=main.cpp pub.cpp

OBJC=$(SOURCE:.cpp=.o)#將.cpp檔案全部替換為.o檔案

EXEC=mytest

 

all:$(OBJC)

$(CC) -o $(EXEC) $(OBJC)

 

.cpp.o:

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

 

clean:

rm -rf $(OBJC)



附-如果一條語句在執行Mackfile檔案時,不想列印出來,則在其前面加上@符號

 

比較專業的Makefile編寫

#樣本1

all: myapp

 

# Which compiler

CC = g++ 

 

# Where are include files kept

INCLUDE = .

 

# Options for development

FLAGS = -g -Wall -ansi

 

# Options for release

# CFLAGS = -O -Wall -ansi

 

.SUFFIXES:.cpp .o

 

SOURCE = main.cpp pub.cpp

OBJC = $(SOURCE:.cpp=.o)

EXEC = myapp

 

$(EXEC): ${OBJC}

$(CC) -o $(EXEC) $(OBJC) 

 

.cpp.o:

$(CC) -o $@ -I$(INCLUDE) $(FLAGS) -c $<

 

clean:

rm -rf $(OBJC) $(EXEC)

 

 

#樣本2-添加了install選項

all: myapp

 

# Which compiler

CC = g++ 

 

# Where to install

INSTDIR = /usr/local/bin/

 

# Where are inlude files kept

INCLUDE = .

 

# Options for development

CFLAGS = -g -Wall -ansi

 

# Options for release

# CFLAGS = -O -Wall -ansi

 

.SUFFIXES: .cpp .o

 

SOURCE = main.cpp pub.cpp

OBJECT = $(SOURCE:.cpp=.o)

EXEC   = myapp

 

$(EXEC): $(OBJECT)

    $(CC) -o $(EXEC) $(OBJECT)

 

.cpp.o:

    $(CC) -I$(INCLUDE) $(CFLAGS) -o $@ -c $<

 

clean:

    -rm -rf $(OBJECT)

 

install: $(EXEC)

    @if [ -d $(INSTDIR) ] ; then \

        cp $(EXEC) $(INSTDIR) ; \

        chmod a+x $(INSTDIR)/$(EXEC) ; \

        chmod og-w $(INSTDIR)/$(EXEC) ; \

        echo "Installed in $(INSTDIR)" ; \

    else \

        echo "Sory, $(INSTDIR) does not exist." ; \

    fi



學習arm是不是剛開始就要學會編寫makefile檔案?

我的經驗是
一:先學會一些命令 學會在linux上安裝卸載軟體
二:你要會Makefile(也就是說你要會在linux下編寫,偵錯工具)
這是必須的
然後就是移植和驅動編寫的學習啦
 
make工程管理器是什?怎編寫makefile檔案

Copyleft_X 回答的很不錯,就是這樣的,我覺得在寫makefile之前,先自己找個好的makefile學習下,在此基礎上修改下,熟悉了makefile怎麼工作之後,對其有了一定的概念上的了結了之後,那再好好看下跟我一起寫makefile這個東西,記住千萬別寫的很複雜,夠用就好了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.