Create directories using make file

前端 未结 9 2032
情深已故
情深已故 2020-11-28 21:27

I\'m a very new to makefiles and i want to create directories using makefile. My project directory is like this

+--Project  
   +--output  
   +--source  
           


        
9条回答
  •  春和景丽
    2020-11-28 22:29

    I've just come up with a fairly reasonable solution that lets you define the files to build and have directories be automatically created. First, define a variable ALL_TARGET_FILES that holds the file name of every file that your makefile will be build. Then use the following code:

    define depend_on_dir
    $(1): | $(dir $(1))
    
    ifndef $(dir $(1))_DIRECTORY_RULE_IS_DEFINED
    $(dir $(1)):
        mkdir -p $$@
    
    $(dir $(1))_DIRECTORY_RULE_IS_DEFINED := 1
    endif
    endef
    
    $(foreach file,$(ALL_TARGET_FILES),$(eval $(call depend_on_dir,$(file))))
    

    Here's how it works. I define a function depend_on_dir which takes a file name and generates a rule that makes the file depend on the directory that contains it and then defines a rule to create that directory if necessary. Then I use foreach to call this function on each file name and eval the result.

    Note that you'll need a version of GNU make that supports eval, which I think is versions 3.81 and higher.

提交回复
热议问题