牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

几个好用的makefile 几乎可以不用修改

Makefile

       makefile 来编译工程,对很多朋友来说都是一件麻烦而痛苦的事情,这里我写了几个 makefile ,专门提供给那些曾经被 makefile 困扰的朋友,根据生成的目标文件不同,我将 makefile 分成了三份:生成可执行文件的 makefile ,生成静态链接库德 makefile ,生成动态链接库的 makefile

       这些 makefile 都很简单,一般都是一看就会用,用法也很容易,只需要把它们拷贝到你的代码的同一目录下,然后就可以使用 make 来生成目标文件了。

       是不是真的有这么神奇?呵呵,你自己用用就知道了。

       当然,如果要用到什么库文件,你还需要修改一些编译参数,这个可以对照我转载的另一篇文章《 GNU make 指南》。

       下面是三个 makefile 的源代码:

 

       1 、生成可执行文件的 makefile

######################################

#

# Generic makefile

#

# by Coon Xu

# email: coonxu@126.com

#

# Copyright (c) 2005 Coon Xu

# All rights reserved.

# 

# No warranty, no liability;

# you use this at your own risk.

#

# You are free to modify and

# distribute this without giving

# credit to the original author.

#

######################################

 

 

#source file

# 源文件,自动找所有 .c .cpp 文件,并将目标定义为同名 .o 文件

SOURCE  := $(wildcard *.c) $(wildcard *.cpp)

OBJS    := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))

 

#target you can change test to what you want

# 目标文件名,输入任意你想要的执行文件名

TARGET  := test

 

#compile and lib parameter

# 编译参数

CC      := gcc

LIBS    :=

LDFLAGS:= 

DEFINES:=

INCLUDE:= -I.

CFLAGS  := -g -Wall -O3 $(DEFINES) $(INCLUDE)

CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H

 

 

#i think you should do anything here

# 下面的基本上不需要做任何改动了

.PHONY : everything objs clean veryclean rebuild

 

everything : $(TARGET)

 

all : $(TARGET)

 

objs : $(OBJS)

 

rebuild: veryclean everything

               

clean :

    rm -fr *.so

    rm -fr *.o

   

veryclean : clean

    rm -fr $(TARGET)

 

$(TARGET) : $(OBJS) 

    $(CC) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

 

       2 、生成静态链接库的 makefile

######################################

#

# Generic Static Library makefile

#

# by Coon Xu

# email: coonxu@126.com

#

# Copyright (c) 2005 Coon Xu

# All rights reserved.

# 

# No warranty, no liability;

# you use this at your own risk.

#

# You are free to modify and

# distribute this without giving

# credit to the original author.

#

######################################

 

#target you can change test to what you want

# 共享库文件名, lib*.a

TARGET  := libtest.a

 

#compile and lib parameter

# 编译参数

CC      := gcc

AR      = ar

RANLIB  = ranlib

LIBS    :=

LDFLAGS:= 

DEFINES:=

INCLUDE:= -I.

CFLAGS  := -g -Wall -O3 $(DEFINES) $(INCLUDE)

CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H

 

#i think you should do anything here

# 下面的基本上不需要做任何改动了

 

#source file

# 源文件,自动找所有 .c .cpp 文件,并将目标定义为同名 .o 文件

SOURCE  := $(wildcard *.c) $(wildcard *.cpp)

OBJS    := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))

 

.PHONY : everything objs clean veryclean rebuild

 

everything : $(TARGET)

 

all : $(TARGET)

 

objs : $(OBJS)

 

rebuild: veryclean everything

               

clean :

    rm -fr *.o

   

veryclean : clean

    rm -fr $(TARGET)

 

$(TARGET) : $(OBJS) 

    $(AR) cru $(TARGET) $(OBJS)

    $(RANLIB) $(TARGET)

 

       3 、生成动态链接库的 makefile

######################################

#

# Generic Share Library makefile

#

# by Coon Xu

# email: coonxu@126.com

#

# Copyright (c) 2005 Coon Xu

# All rights reserved.

# 

# No warranty, no liability;

# you use this at your own risk.

#

# You are free to modify and

# distribute this without giving

# credit to the original author.

#

######################################

 

#target you can change test to what you want

# 共享库文件名, lib*.so

TARGET  := libtest.so

 

#compile and lib parameter

# 编译参数

CC      := gcc

LIBS    :=

LDFLAGS:= 

DEFINES:=

INCLUDE:= -I.

CFLAGS  := -g -Wall -O3 $(DEFINES) $(INCLUDE)

CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H

SHARE   := -fPIC -shared -o

 

#i think you should do anything here

# 下面的基本上不需要做任何改动了

 

#source file

# 源文件,自动找所有 .c .cpp 文件,并将目标定义为同名 .o 文件

SOURCE  := $(wildcard *.c) $(wildcard *.cpp)

OBJS    := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))

 

.PHONY : everything objs clean veryclean rebuild

 

everything : $(TARGET)

 

all : $(TARGET)

 

objs : $(OBJS)

 

rebuild: veryclean everything

               

clean :

    rm -fr *.o

   

veryclean : clean

    rm -fr $(TARGET)

 

$(TARGET) : $(OBJS) 

    $(CC) $(CXXFLAGS) $(SHARE) $@ $(OBJS) $(LDFLAGS) $(LIBS)

posted on 2007-02-08 15:34 杨粼波 阅读(824) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理