title: Set output path when compiling a kernel module?
body: <p>Is it possible to provide an output folder when building a kernel module?
  These questions are very similar to mine but the answers are non conclusive  <a href="https://stackoverflow.com/questions/12244979/build-kernel-module-into-a-specific-directory">Link1</a>, <a href="https://stackoverflow.com/questions/11676391/redirecting-output-of-linux-module-build?rq=1">Link2</a> (I have tested these proposals without success). </p>
  
  <p>I have the following project structure and would like to put all outputs from the compiling of the kernel module into the <strong>build</strong> folder.</p>
  
  <pre><code>Makefile
  src/
     -main.c
  build/
  </code></pre>
  
  <p>This is my current Makefile:</p>
  
  <pre><code># name of the module to be built
  MODULE_NAME ?= test_module
  
  BASE := .
  # define all module sources
  SRCS :=                                 \
      src/main.c                          
  
  # extract required object files
  OBJ_SRCS :=  $(SRCS:.c=.o)
  
  # define path to directories containing header files
  INCLUDE_DIRS =                          \
      -I$(src)/src                        
  
  # Products from the 'make all' command should be placed in this folder
  BUILD_DIR := $(BASE)/build
  
  # ccflags to pass at compile time
  ccflags-y :=                            \
      $(INCLUDE_DIRS)                     
  
  # setup kbuild stuff
  obj-m += $(MODULE_NAME).o
  $(MODULE_NAME)-y := $(OBJ_SRCS)
  
  $(MAKE) = make
  KERNEL_DIR=/lib/modules/$(shell uname -r)/build
  
  all:
      $(MAKE) -C $(KERNEL_DIR)  M=$(PWD) modules
  clean:
      $(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
  </code></pre>
  
  <p>I have tried to add O=$(BUILD_DIR) as (as stated in the link above without success).</p>
  
  <pre><code>$(MAKE) -C $(KERNEL_DIR) O=$(BUILD_DIR) M=$(PWD) modules
  </code></pre>
  
  <p>Can anyone confirm if it is possible to redirect to output(<strong>the files generated during the compilation</strong>) when compiling the kernel module or not? (A suggestion would be much appreciated)</p>
  
  <p>Is the only solution to put the Makefile in the build folder and update the all paths in the Makefile?</p>
  
  <p>Thanks!</p>