Xiaobo Sun

Eclipse-Unix http://umlfact.berlios.de/~s_xsun/

Makefile example

#libxml2 and glibmm are not neccessary to compile the main.cpp
#macro
libxml2:=../../libxml2
libxml++:=..
glibmm:=../../glibmm
#adds include directives
CC:=g++
CCINCLUDE:= -I$(libxml++)/include/libxml++-2.6 -I$(libxml++)/lib/libxml++-2.6/include "
        -I$(libxml2)/include -I$(glibmm)/include
#linking
LD:=g++
LDSTDLIBS:= -L$(libxml++)/lib -L$(libxml2)/lib -L$(glibmm)/lib
LDLIBS:= -lxml++-2.6
LDLIBFLAGS:= -shared $(LDLIBS) $(LDSTDLIBS)
LDEXEFLAGS:= $(LDSTDLIBS) $(LDLIBS)
#extending library path
#export VAR:=... means make it effective in the subprocess(Makefile)
#export LD_LIBRARY_PATH:=... doesn't work because it's only effective in the subprocesses, not parentprocess(shell)
#therefore the LD_RUN_PATH which allocates dynamic libs to the exe file must be set and export to take effect in the ld cmd
LD_LIBRARY_PATH:=$(libxml++)/lib:$(LD_LIBRARY_PATH)
export LD_RUN_PATH:=$(LD_LIBRARY_PATH)
#list of source files for building the target
SRC:= main.cpp
OBJ:=$(patsubst %.cpp,%.o,$(filter %.cpp,$(SRC)))
#targets
# $^ everyone behinds the : , and $< first one behinds the :
# $(OBJ): %.o: %.cpp is another common implicit rule is for the construction of .o files out of .cpp
.PHONY: all clean
all: $(OBJ)
        $(LD) $(LDEXEFLAGS) $^ -o $@

$(OBJ): %.o: %.cpp
        $(CC) $(CCINCLUDE) -c $< -o $@
clean:
        rm -f $(OBJ) all *~ \.*.swp

posted on 2008-10-09 13:55 Xiaobo Sun 阅读(979) 评论(3)  编辑  收藏 所属分类: C++

评论

# re: Makefile example 2008-10-09 13:57 Xiaobo Sun

# macro
GSOAPPATH=../../
CCINCLUDE= -I$(GSOAPPATH)
#CCINCLUDE+= -I$(RESPONSEPATH)
CCFLAGS= -Wall -g
OBJECTS= soapServiceRequestIFSOAPService.o soapC.o stdsoap2.o \
soapServiceResponseIFSOAPProxy.o
# targets
all : server++ client++

server++ : server.cpp $(OBJECTS)
g++ $(CCFLAGS) $(CCINCLUDE) $^ -o $@

client++ : client.cpp soapC.o stdsoap2.o soapServiceResponseIFSOAPProxy.o
g++ $(CCFLAGS) $(CCINCLUDE) $^ -o $@

soapServiceRequestIFSOAPService.o : soapServiceRequestIFSOAPService.cpp
g++ -c $(CCINCLUDE) $^

soapC.o : soapC.cpp
g++ -c $(CCINCLUDE) $^

stdsoap2.o : $(GSOAPPATH)stdsoap2.cpp
g++ -c $(CCINCLUDE) $^

soapServiceResponseIFSOAPProxy.o : soapServiceResponseIFSOAPProxy.cpp
g++ -c $(CCINCLUDE) $^

clean :
rm -f server++ client++ *.o *~ *.swp

.PHONY : all clean
  回复  更多评论   

# re: Makefile example 2008-10-09 14:07 Xiaobo Sun

看一个例子:
objects = foo.o bar.o
all: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
上面的例子中,指明了我们的目标从$object中获取,“%.o”表明要所有以“.o”结尾的
目标,也就是“foo.o bar.o”,也就是变量$object集合的模式,而依赖模式“%.c”则取模
式“%.o”的“%”,也就是“foo bar”,并为其加下“.c”的后缀,于是,我们的依赖目标
就是“foo.c bar.c”。而命令中的“$<”和“$@”则是自动化变量,“$<”表示所有的依赖
目标集(也就是“foo.c bar.c”),“$@”表示目标集(也就是“foo.o bar.o”)。于是,
上面的规则展开后等价于下面的规则:
foo.o : foo.c
$(CC) -c $(CFLAGS) foo.c -o foo.o
bar.o : bar.c
$(CC) -c $(CFLAGS) bar.c -o bar.o
试想,如果我们的“%.o”有几百个,那种我们只要用这种很简单的“静态模式规则”就
可以写完一堆规则,实在是太有效率了  回复  更多评论   

# re: Makefile example 2008-10-09 15:08 Xiaobo Sun

如果你要传递变量到下级Makefile中,那么你可以使用这样的声明:
export <variable ...>;

export variable := value
其等价于:
variable := value
export variable

export variable += value
其等价于:
variable += value
export variable  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜