Create directories using make file

前端 未结 9 2075
情深已故
情深已故 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:08

    In my opinion, directories should not be considered targets of your makefile, either in technical or in design sense. You should create files and if a file creation needs a new directory then quietly create the directory within the rule for the relevant file.

    If you're targeting a usual or "patterned" file, just use make's internal variable $(@D), that means "the directory the current target resides in" (cmp. with $@ for the target). For example,

    $(OUT_O_DIR)/%.o: %.cpp
            @mkdir -p $(@D)
            @$(CC) -c $< -o $@
    
    title: $(OBJS)
    

    Then, you're effectively doing the same: create directories for all $(OBJS), but you'll do it in a less complicated way.

    The same policy (files are targets, directories never are) is used in various applications. For example, git revision control system doesn't store directories.


    Note: If you're going to use it, it might be useful to introduce a convenience variable and utilize make's expansion rules.

    dir_guard=@mkdir -p $(@D)
    
    $(OUT_O_DIR)/%.o: %.cpp
            $(dir_guard)
            @$(CC) -c $< -o $@
    
    $(OUT_O_DIR_DEBUG)/%.o: %.cpp
            $(dir_guard)
            @$(CC) -g -c $< -o $@
    
    title: $(OBJS)
    

提交回复
热议问题