﻿{"id":420,"date":"2021-03-25T20:10:29","date_gmt":"2021-03-25T12:10:29","guid":{"rendered":"https:\/\/dt9025a.xyz\/?p=420"},"modified":"2021-03-25T20:10:29","modified_gmt":"2021-03-25T12:10:29","slug":"makefile","status":"publish","type":"post","link":"https:\/\/dt9025a.top\/?p=420","title":{"rendered":"Makefile"},"content":{"rendered":"\n<p>\u4e00\u5c0f\u65f6\uff0c\u5b66\u4e86\u4e2a\u5bc2\u5bde\u3002<\/p>\n\n\n\n<p>\u963f\u91cc\u7684demo\u5c31\u662f\u4e13\u4e1a\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#@:\u9759\u9ed8\u6267\u884c\nQ := @\n\n#pwd\u6307\u4ee4\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e3a\u9876\u5c42\u76ee\u5f55\nTOP_DIR := $(shell pwd)\n#demos\u76ee\u5f55\nDEMOS_DIR := demos\n#\u8f93\u51fa\u76ee\u5f55\nOUT_DIR := output\n\n#C Compiler\nCC := gcc\n\n#\u7f16\u8bd1\u9009\u9879\nBLD_CFLAGS := $(CFLAGS) -Wall -Werror\n#\u8f93\u51fa\nBLD_CFLAGS += -Os\n\n#\u52a0\u5165\u6307\u5b9alibrary pthread\nBLD_LDFLAGS := $(LDFLAGS) -lpthread\n\n#\u67e5\u627e \u5f53\u524d\u76ee\u5f55\u4e0b \u683c\u5f0f\u4e3aDIR \u4e14\u540d\u79f0\u4e0d\u4e3ademos \u4f5c\u4e3aSRC_DIR\nSRC_DIR := $(shell find . -type d \\( ! -name demos \\))\n\n#\u8bbe\u7f6eHeaders\u8def\u5f84\nHDR_DIR := $(SRC_DIR)\n#\u7f16\u8bd1\u9009\u9879\u6dfb\u52a0Include\u76ee\u5f55:HDR_DIR\nBLD_CFLAGS += $(addprefix -I,$(HDR_DIR))\n\n#\u5728SRC_DIR\u4e0b\u5bfb\u627e\u8def\u5f84\u4e0d\u4e3a*\/demos\/*.c\u4e14\u540d\u79f0\u4e3a*.c\u7684\u6587\u4ef6\u505a\u6e90\u6587\u4ef6\nSRC_FILES := $(shell find $(SRC_DIR) -not -path \"*\/demos\/*.c\" -name \"*.c\")\n#\u5c06SRC_FILES\u7684\u540e\u7f00\u540d\u6539\u4e3a.o\u4f5c\u4e3aobj\u6587\u4ef6\u540d\nOBJ_FILES := $(SRC_FILES:.c=.o)\n#\u5c06obj\u6587\u4ef6\u540d\u52a0\u4e0a\u8f93\u51fa\u76ee\u5f55\u524d\u7f00\nOBJ_FILES := $(addprefix $(OUT_DIR)\/,$(OBJ_FILES))\n\n#\u67e5\u627edemos\/*_demo.c, \u5c06.c\u53bb\u6389, \u66ff\u6362_\u4e3a-\u4f5c\u4e3a\u8f93\u51fa\u6587\u4ef6\u540d\nPROG_TARGET := $(subst _,-,$(patsubst %.c,%,$(wildcard demos\/*_demo.c)))\n\n#\u6267\u884c: \u540e\u9762\u4e24\u4e2a\u4f9d\u8d56\u9879\u4e2d\u6240\u6709\u5185\u5bb9\nall: prepare $(PROG_TARGET)\n\n#\u521b\u5efa\u4e00\u4e2aoutput\u76ee\u5f55\nprepare:\n\t$(Q)mkdir -p output\n\n#\u7f16\u8bd1\u76ee\u6807, \u4f9d\u8d56\u4e3aOBJ_FILES\n$(PROG_TARGET): $(OBJ_FILES)\n\t#\u8bf4\u8bdd\n\t$(Q)echo \"+ Linking $(OUT_DIR)\/$(notdir $@) ...\"\n\t#\u5efa\u7acb\u5177\u6709\u76ee\u6807\u540d\u79f0\u7684\u6587\u4ef6\u5939\n\t$(Q)mkdir -p $(dir $@)\n\t#\u6267\u884cgcc\u6307\u4ee4\n\t#gcc -o $(PROG_TARGET)\n\t$(Q)$(CC) -o $@ \\\n\t\t# \u5c06\u66f4\u540d\u4e3a.o\u7684.c\u6587\u4ef6\u66f4\u540d\u56de\u6765 \u5220\u53bb\u8def\u5f84\n\t    $(patsubst $(OUT_DIR)\/%,%,$(addsuffix .c,$(subst $(notdir $@),$(subst -,_,$(notdir $@)),$@))) \\\n\t\t#\u52a0\u5165\u7f16\u8bd1\u9009\u9879\u548c\u5e93\n\t    $(BLD_CFLAGS) $^ $(BLD_LDFLAGS)\n\t#\u628a\u7f16\u8bd1\u5b8c\u7684\u6587\u4ef6\u79fb\u5230\u8f93\u51fa\u76ee\u5f55\n\t$(Q)mv $@ $(OUT_DIR)\n\n#\u7f16\u8bd1\u76ee\u6807.o\u6587\u4ef6, \u4f9d\u8d56\u4e3a.c\n$(OUT_DIR)\/%.o: %.c\n\t#\u8bf4\u8bdd\n\t$(Q)echo \": Compiling $&lt; ...\"\n\t#\u5efa\u7acb\u6587\u4ef6\u5939\n\t$(Q)mkdir -p $(OUT_DIR)\/$(dir $&lt;)\n\t#\u7f16\u8bd1c\u6587\u4ef6\n\t$(Q)$(CC) -o $@ -c $&lt; $(BLD_CFLAGS)\n\n#make clean\u5e72\u8fd9\u4e2a\nclean:\n\t$(Q)rm -rf $(OUT_DIR)\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u5c0f\u65f6\uff0c\u5b66\u4e86\u4e2a\u5bc2\u5bde\u3002 \u963f\u91cc\u7684demo\u5c31\u662f\u4e13\u4e1a\u3002 #@:\u9759\u9ed8\u6267\u884c Q := @ #pwd\u6307\u4ee4\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e3a\u9876&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/dt9025a.top\/?p=420\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-420","post","type-post","status-publish","format-standard","hentry","category-linux-and-so-on"],"_links":{"self":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts\/420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=420"}],"version-history":[{"count":0,"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts\/420\/revisions"}],"wp:attachment":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}