How do I write the 'cd' command in a makefile?

后端 未结 6 445
时光取名叫无心
时光取名叫无心 2020-11-29 14:53

For example, I have something like this in my makefile:

all:
     cd some_directory

But when I typed make I saw only \'cd some

6条回答
  •  迷失自我
    2020-11-29 15:17

    Here's a cute trick to deal with directories and make. Instead of using multiline strings, or "cd ;" on each command, define a simple chdir function as so:

    CHDIR_SHELL := $(SHELL)
    define chdir
       $(eval _D=$(firstword $(1) $(@D)))
       $(info $(MAKE): cd $(_D)) $(eval SHELL = cd $(_D); $(CHDIR_SHELL))
    endef
    

    Then all you have to do is call it in your rule as so:

    all:
              $(call chdir,some_dir)
              echo "I'm now always in some_dir"
              gcc -Wall -o myTest myTest.c
    

    You can even do the following:

    some_dir/myTest:
              $(call chdir)
              echo "I'm now always in some_dir"
              gcc -Wall -o myTest myTest.c
    

提交回复
热议问题