user-0 提问: gcc的makefile

回复:
user: user-1
content: 引用 1 楼 lisong694767315 的回复: obj = main.c my/my.c include/my.h main: $(obj) 	gcc -o main $(obj)    新增一个文件就要改makefile?

user: user-2
content: 搜“跟我一起写makefile”

user: user-3
content: 推荐你用eclipse或者其他编译环境生成一个makefile然后自己改一下

user: user-4
content: 接楼上：  Makefile中以下三行指定源代码、头文件、中间文件的目录  src_dir := src inc_dir := include obj_dir := obj  以下三行是设置编译器参数的 CFLAGS    := -fPIC -Wall -g -DTHREADED -I "/usr/local/include" \                          $(foreach d,$(inc_dir), -I "$d") CPPFLAGS  := -fPIC -Wall -g -DTHREADED -I "/usr/local/include" \                          $(foreach d,$(inc_dir), -I "$d") LDFLAGS   := -L "/usr/local/lib" LOADLIBES := --shared -lzookeeper_mt -lm -lpthread

user: user-4
content: 建议好好的看一下《Managing Projects with GNU Makefile》这本书，有英文版和中文版的。  下面是我的makefile，还算通用，就是写的很复杂。共两个文件，一个是makefile，另外一个是make.rules。两个文件缺一不可，我就是看了上面的书写出来的。另外，再给你一个建议，你可以用cmake，cmake的语法不难，它是自动生成Makefile的工具。  <makefile>  ifdef ComSpec EMPTY     := RM        := del DELIMITER := \$(EMPTY) TARGET    := libzooclient.dll else RM        := rm DELIMITER := / TARGET    := libzooclient.so endif  all: $(TARGET)  src_fmt := *.c *.C *.cc *.CC *.cpp *.cxx src_dir := src inc_dir := include obj_dir := obj  CFLAGS    := -fPIC -Wall -g -DTHREADED -I "/usr/local/include" \ 						 $(foreach d,$(inc_dir), -I "$d") CPPFLAGS  := -fPIC -Wall -g -DTHREADED -I "/usr/local/include" \ 						 $(foreach d,$(inc_dir), -I "$d") LDFLAGS   := -L "/usr/local/lib" LOADLIBES := --shared -lzookeeper_mt -lm -lpthread  # List all sources now. src     := $(foreach d,$(src_dir),\  $(foreach f,$(src_fmt),\   $(if $(wildcard $d/$f),$(eval src+=$(wildcard $d/$f)))))  # Check whether sources contains cxx files. cplusplus := $(filter-out %.c,$(src)) # Check whether builds archive. archive := $(filter %.a,$(TARGET))  include make.rules  ifneq ($(MAKECMDGOALS),"clean") ifneq ($(MAKECMDGOALS),"distclean") # Include dependent files if exist. -include $(addprefix $(obj_dir)/,$(addsuffix .d, $(notdir $(src)))) # Generate compile rules from source to object file. # To "C" files, use gcc, otherwise picking g++. $(foreach f,$(src),\  $(if $(filter %.c,$f),\   $(eval $(call rule-generator-c2o,$(obj_dir),$f)),\   $(eval $(call rule-generator-cxx2o,$(obj_dir),$f)))) # Generate compile rules from object files to executable. # # if source-file(s)-exists #		if target-is-xxx.a #			ar rv $@ $^ #   else if use-c++ #     g++ $^ -o $@ #   else #     gcc $^ -o $@ #		endif # endif $(if $(src),\  $(if $(archive),$(eval $(call archive-target,$(TARGET))),\   $(if $(cplusplus),\    $(eval $(call link-target-cxx,$(TARGET))),\    $(eval $(call link-target-c,$(TARGET))))))  endif endif  prompt_clean := @echo "Nothing to clean." prompt_dclean:= @echo "Nothing to distclean."  objects := $(foreach d,$(obj_dir),\  $(if $(wildcard $d/*.o), $(eval objects+=$(wildcard $d/*.o)))) depends := $(foreach d,$(obj_dir),\  $(if $(wildcard $d/*.d), $(eval depends+=$(wildcard $d/*.d))))  clean_cmd := $(if $(objects)$(depends),\               $(RM) $(objects) $(depends),\               $(prompt_clean)) target := $(if $(wildcard $(TARGET)), $(eval target=$(TARGET))) distclean_cmd := $(if $(objects)$(depends)$(target),\                   $(RM) $(objects) $(depends) $(target),\                   $(prompt_dclean))  clean: 	$(clean_cmd) distclean: 	$(distclean_cmd)  .PHONY: all clean distclean   <make.rules>  # Compiler specific variables. CC         := gcc CXX        := g++ COMPILE.c   = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c COMPILE.cxx = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c LINK.c.o    = $(CC) $(LDFLAGS) $(TARGET_ARCH) LINK.cxx.o  = $(CXX) $(LDFLAGS) $(TARGET_ARCH) AR         := ar ARFLAGS     = rv  # Store all object files while generating rules. allobjects :=  # ******************************** # # ############### C ###############   # $(call depend-generator-c,<target:$1>,<dependfilepath:$2>,<sourcefilepath:$3> # Example: $(call depend-generator,obj/main.c.o,obj/main.c.d,src/main.c) define depend-generator-c $(CC) $(CFLAGS) -MM -MT $1 -MF $2 -MP $3 endef  # Automatic rules from *.c to *.o # $(call rule-generator-c2o,<objdir:$1>,<sourcefilepath:$2>) define rule-generator-c2o allobjects += $1/$(notdir $2).o $1/$(notdir $2).o: $2 	$$(call depend-generator-c,$1/$(notdir $2).o,$1/$(notdir $2).d,$2) 	$(COMPILE.c) $$< -o $$@ endef  # Automatic rules from *.o to executables # $(call link-target-c,<target:$1>) define link-target-c $1: $(allobjects) 	$(LINK.c.o) $$^ $(LDLIBS) $(LOADLIBES) -o $$@ endef  # ******************************** # # ############## C++ ##############  # $(call depend-generator-cxx,<target:$1>,<dependfilename:$2>,<sourcefilename:$3> # Example: $(call depend-generator,obj/main.cxx.o,obj/main.cxx.d,src/main.cxx) define depend-generator-cxx $(CXX) $(CPPFLAGS) -MM -MT $1 -MF $2 -MP $3 endef  # Automatic rules from *.cxx to *.o # $(call rule-generator-cxx2o,<objdir:$1>,<sourcefile:$2>) define rule-generator-cxx2o allobjects += $1/$(notdir $2).o $1/$(notdir $2).o: $2 	$$(call depend-generator-cxx,$1/$(notdir $2).o,$1/$(notdir $2).d,$2) 	$(COMPILE.cxx) $$< -o $$@ endef  # Automatic rules from *.o to executables # $(call link-target-cxx,<target:$1>) define link-target-cxx $1: $(allobjects) 	$(LINK.cxx.o) $$^ $(LDLIBS) $(LOADLIBES) -o $$@ endef  # *********************************** # # ############## Archive ############## # Automatic rule from *.o to archive define archive-target $1: $(allobjects) 	$(AR) $(ARFLAGS) $$@ $$^ endef

user: user-5
content: 试试 GNU Make 的 vpath 指令

user: user-6
content: obj = main.c my/my.c include/my.h main: $(obj) 	gcc -o main $(obj)