{"id":276,"date":"2011-12-12T09:11:34","date_gmt":"2011-12-12T14:11:34","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=276"},"modified":"2011-12-12T09:14:43","modified_gmt":"2011-12-12T14:14:43","slug":"makefile%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2011\/12\/makefile%e6%8a%80%e5%b7%a7\/","title":{"rendered":"Makefile\u6280\u5de7\u4e09\u5219"},"content":{"rendered":"<p>Three Tricks to use Makefile Efficiently<\/p>\n<p>\u6700\u8fd1\u8bfb<a href=\"https:\/\/github.com\/cloudwu\/pbc\" title=\"Protocol Buffer For C\">Protocol Buffer for C<\/a>\uff0c\u53d1\u73b0\u4e00\u4e9bMakefile\u7684\u6280\u5de7<\/p>\n<p>1. -MMD flag<br \/>\nMakefile\u6bd4\u8f83\u9ebb\u70e6\u7684\u662f\u5199dependency\u3002\u5e38\u7528\u6280\u5de7\u662f\u901a\u8fc7g++ -MM\u548csed\u811a\u672c\u4ece.cpp\u6e90\u6587\u4ef6\u751f\u6210.d\u6587\u4ef6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u89e3\u51b3\u81ea\u52a8\u751f\u6210\u4f9d\u8d56\u5173\u7cfb\u7684\u95ee\u9898\uff0c\u4f46\u5176\u5b9e\u6709\u66f4\u597d\u7684\u65b9\u6cd5\uff1a<br \/>\ng++ -MMD -c XXX.c<br \/>\n\u4e4b\u540e\u5728Makefile\u91cc\u6dfb\u52a0\uff1a<br \/>\n-include XXX.d<\/p>\n<p>g++\u7684\u6587\u6863\u63d0\u5230-MMD:<\/p>\n<pre>\r\n-MMD\r\n    Like -MD except mention only user header files, not system header files. \r\n<\/pre>\n<p>\u9664\u4e86-MMD\u5916\uff0c\u6709\u610f\u601d\u7684g++\u53c2\u6570\uff0c\u53ef\u4ee5\u53c2\u89c1<a href=\"http:\/\/tombarta.wordpress.com\/2008\/05\/25\/gcc-flags\/\" title=\"gcc flags\">gcc flags<\/a> \u3002\u53e6\u5916\u8fd9\u7bc7\u8be6\u7ec6\u8bb2\u89e3Makefile dependency\u7684\u6587\u7ae0<a href=\"http:\/\/mad-scientist.net\/make\/autodep.html\" title=\"Makefile Autodependency\">Makefile Autodependency<\/a>\u3002<\/p>\n<p>2. Order-only prerequisites<br \/>\n<a href=\"http:\/\/www.gnu.org\/s\/make\/manual\/make.html#Prerequisite-Types\" title=\"order-only-prerequisites\">order-only-prerequisites<\/a><br \/>\n\u7528Makefile\u91cc\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a<\/p>\n<pre>\r\n     OBJDIR := objdir\r\n     OBJS := $(addprefix $(OBJDIR)\/,foo.o bar.o baz.o)\r\n     \r\n     $(OBJDIR)\/%.o : %.c\r\n             $(COMPILE.c) $(OUTPUT_OPTION) $<\r\n     \r\n     all: $(OBJS)\r\n     \r\n     $(OBJS): | $(OBJDIR)\r\n     \r\n     $(OBJDIR):\r\n             mkdir $(OBJDIR)\r\n<\/pre>\n<p>$(OBJS)\u9700\u8981$(OBJDIR)\uff0c\u6240\u4ee5\u9700\u8981\u628a$(OBJS)\u653e\u5728$(OBJDIR)\u524d\u9762\uff1b\u4f46\u5728\u5efa\u7acb$(OBJDIR)\u4e4b\u540e\uff0c$(OBJ)\u5c31\u4e0d\u5728\u9700\u8981\u6839\u636e$(OBJDIR)\u7684\u65f6\u95f4\u8c03\u6574\u4e86\u3002<\/p>\n<p>3. Multiple line variables \uff08Eval function)<br \/>\nMakefile\u91cc\u7684\u53d8\u91cf\u53ef\u4ee5\u6709\u591a\u884c\uff0c\u8fd9\u79cd\u53d8\u91cf\u8981\u7528define\u6765\u5b9a\u4e49\u3002<br \/>\n\u4f7f\u7528\u8fd9\u79cd\u53d8\u91cf\u6709\u4e24\u79cd\u573a\u5408\uff1a\uff081\uff09<a href=\"http:\/\/www.gnu.org\/s\/make\/manual\/make.html#Canned-Recipes\" title=\"Canned Recipes\">Canned Recipes<\/a>\uff1b \uff082\uff09<a href=\"http:\/\/www.gnu.org\/s\/make\/manual\/make.html#Eval-Function\" title=\"Eval-Function\">Eval-Function<\/a><\/p>\n<p>\u7b2c\u4e00\u79cd\u5c31\u662f\u628a\u5e38\u7528\u7684\u4e00\u7ec4\u547d\u4ee4\u5305\u88c5\u5728\u4e00\u8d77\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre>\r\n     define run-yacc =\r\n     yacc $(firstword $^)\r\n     mv y.tab.c $@\r\n     endef\r\n<\/pre>\n<p>\u4e4b\u540e\u8c03\u7528\uff1a<\/p>\n<pre>\r\n     foo.c : foo.y\r\n             $(run-yacc)\r\n<\/pre>\n<p>\u7b2c\u4e8c\u79cd\u66f4\u52a0\u5e38\u7528\uff0c\u6bd4\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>\r\n     PROGRAMS    = server client\r\n     \r\n     server_OBJS = server.o server_priv.o server_access.o\r\n     server_LIBS = priv protocol\r\n     \r\n     client_OBJS = client.o client_api.o client_mem.o\r\n     client_LIBS = protocol\r\n     \r\n     # Everything after this is generic\r\n     \r\n     .PHONY: all\r\n     all: $(PROGRAMS)\r\n     \r\n     define PROGRAM_template =\r\n      $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)\r\n      ALL_OBJS   += $$($(1)_OBJS)\r\n     endef\r\n     \r\n     $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))\r\n     \r\n     $(PROGRAMS):\r\n             $(LINK.o) $^ $(LDLIBS) -o $@\r\n     \r\n     clean:\r\n             rm -f $(ALL_OBJS) $(PROGRAMS)\r\n<\/pre>\n<p>\u9996\u5148\u4e0a\u9762\u6ce8\u91ca\u884c\u4ee5\u4e0b\u90fd\u662f\u901a\u7528\uff08generic\uff09\u7684\u3002<br \/>\n\u5176\u6b21eval\u4f1a\u4e24\u6b21\u5c55\u5f00(expand)\u53d8\u91cf\u540d\uff0c\u6240\u4ee5$(1)\u4f1a\u88ab\u6269\u5c55\u6210prog\uff08\u6bd4\u5982server\uff09\uff0c$$($(1)_OBJS)\u4f1a\u88ab\u6269\u5c55\u6210$(server_OBJS)\uff0c\u4e4b\u540e\u4f1a\u518d\u6b21\u88ab\u6269\u5c55\u6210server.o server_priv.o server_access.o<\/p>\n<p>\u6700\u7cbe\u5f69\u7684\u5730\u65b9\u662f\u5982\u4f55\u628a\u4e0a\u97623\u4e2a\u6280\u5de7\u8054\u5408\u8d77\u6765\u3002\u4e0b\u9762\u770b\u4e00\u4e0bcloudwu\u7684<a href=\"https:\/\/github.com\/cloudwu\/pbc\" title=\"Protocol Buffer For C\">Protocol Buffer for C<\/a>\u9879\u76ee\u7684Makefile\u7247\u6bb5\uff1a<\/p>\n<pre>\r\nBUILD = build\r\n\r\nLIBSRCS = context.c varint.c array.c pattern.c register.c proto.c map.c alloc.c rmessage.c wmessage.c bootstrap.c stringpool.c\r\nLIBNAME = libpbc.$(SO)\r\n\r\nTESTSRCS = addressbook.c pattern.c\r\nPROTOSRCS = addressbook.proto descriptor.proto\r\n\r\nBUILD_O = $(BUILD)\/o\r\n\r\nall : lib test\r\n\r\nlib : $(LIBNAME)\r\n\r\nclean :\r\n\trm -rf $(BUILD)\r\n\r\n$(BUILD) : $(BUILD_O)\r\n\r\n$(BUILD_O) :\r\n\tmkdir -p $@\r\n\r\nLIB_O :=\r\n\r\ndefine BUILD_temp\r\n  TAR :=  $(BUILD_O)\/$(notdir $(basename $(1)))\r\n  LIB_O := $(LIB_O) $$(TAR).o\r\n  $$(TAR).o : | $(BUILD_O)\r\n  -include $$(TAR).d\r\n  $$(TAR).o : src\/$(1)\r\n\t$(CC) $(CFLAGS) -c -Isrc -I. -fPIC -o $$@ -MMD $$<\r\nendef\r\n\r\n$(foreach s,$(LIBSRCS),$(eval $(call BUILD_temp,$(s))))\r\n\r\n$(LIBNAME) : $(LIB_O)\r\n\tcd $(BUILD) &#038;&#038; $(CC) --shared -o $(LIBNAME) $(addprefix ..\/,$^)\r\n<\/pre>\n<p>\u6709\u610f\u601d\u7684\u5730\u65b9\u662fBUILD_temp\uff0c\u5c06\u6bcf\u4e00\u4e2a$(1)\u7684\u503c\u8d4b\u7ed9TAR\uff0c\u4e4b\u540e$(TAR)\u5728$(BUILD_O)\u76ee\u5f55\u4e0b\u7f16\u8bd1\uff0c\u7f16\u8bd1\u65f6\u7684\u4f9d\u8d56\u5173\u7cfb\u88ab-include\u5230Makefile\u5185\u90e8\uff08include\u524d\u7684\u51cf\u53f7,-,\u8868\u793a\u8981include\u7684\u6587\u4ef6\u5373\u4f7f\u4e0d\u5b58\u5728\u4e5f\u4e0d\u62a5\u9519\uff09\uff0c\u540c\u65f6\u628a\u7f16\u8bd1\u597d\u7684$(TAR).o\u52a0\u5165\u5230LIB_O\u53d8\u91cf\u4e2d\uff08+=\u8d4b\u503c\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Three Tricks to use Makefile Efficiently \u6700\u8fd1\u8bfbProtocol Buffer for C\uff0c\u53d1\u73b0\u4e00\u4e9bMakefile\u7684\u6280\u5de7 1. -MMD flag Makefile\u6bd4\u8f83\u9ebb\u70e6\u7684\u662f\u5199dependency\u3002\u5e38\u7528\u6280\u5de7\u662f\u901a\u8fc7g++ -MM\u548csed\u811a\u672c\u4ece.cpp\u6e90\u6587\u4ef6\u751f\u6210.d\u6587\u4ef6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u89e3\u51b3\u81ea\u52a8\u751f\u6210\u4f9d\u8d56\u5173\u7cfb\u7684\u95ee\u9898\uff0c\u4f46\u5176\u5b9e\u6709\u66f4\u597d\u7684\u65b9\u6cd5\uff1a g++ -MMD -c XXX.c \u4e4b\u540e\u5728Makefile\u91cc\u6dfb\u52a0\uff1a -include XXX.d g++\u7684\u6587\u6863\u63d0\u5230-MMD: -MMD Like -MD except mention only user header files, not system header files. \u9664\u4e86-MMD\u5916\uff0c\u6709\u610f\u601d\u7684g++\u53c2\u6570\uff0c\u53ef\u4ee5\u53c2\u89c1gcc flags \u3002\u53e6\u5916\u8fd9\u7bc7\u8be6\u7ec6\u8bb2\u89e3Makefile dependency\u7684\u6587\u7ae0Makefile Autodependency\u3002 2. Order-only prerequisites order-only-prerequisites \u7528Makefile\u91cc\u7684\u4f8b\u5b50\u6765\u8bf4\uff1a OBJDIR := objdir OBJS := $(addprefix $(OBJDIR)\/,foo.o bar.o baz.o) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[73,74,31,72,76,75],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-code","tag-g","tag-gcc","tag-google","tag-makefile","tag-mmd","tag-protocol-buffer"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}